How to use assemblyscript - 10 common examples

To help you get started, we’ve selected a few assemblyscript examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github holochain / hdk-assemblyscript / transforms / decorator-deserializable.js View on Github external
entrySrc.statements.forEach(s => {
    if (
      s.kind === NodeKind.CLASSDECLARATION &&
      s.decorators &&
      s.decorators.length &&
      s.decorators.some(d => d.name.text === "deserializable")
    ) {
      const name = s.name.text
      const marshalCode = buildMarshal(name, deserializableClasses[name])
      console.log(marshalCode)
      const marshalStmt = parseStatements(entrySrc, marshalCode)[0]
      const method = Node.createMethodDeclaration(
        marshalStmt.name,
        null,
        marshalStmt.signature,
        marshalStmt.body,
        null,
        CommonFlags.STATIC,
        entrySrc.range  // TODO
github holochain / hdk-assemblyscript / transforms / decorator-deserializable.js View on Github external
s.kind === NodeKind.CLASSDECLARATION &&
      s.decorators &&
      s.decorators.length &&
      s.decorators.some(d => d.name.text === "deserializable")
    ) {
      const name = s.name.text
      const marshalCode = buildMarshal(name, deserializableClasses[name])
      console.log(marshalCode)
      const marshalStmt = parseStatements(entrySrc, marshalCode)[0]
      const method = Node.createMethodDeclaration(
        marshalStmt.name,
        null,
        marshalStmt.signature,
        marshalStmt.body,
        null,
        CommonFlags.STATIC,
        entrySrc.range  // TODO
      )
      // s.members.push(method)
      entrySrc.statements.push(marshalStmt)
    }
  })
github holochain / hdk-assemblyscript / transforms / decorator-deserializable.js View on Github external
entrySrc.statements.forEach(s => {
    if (
      s.kind === NodeKind.CLASSDECLARATION &&
      s.decorators &&
      s.decorators.length &&
      s.decorators.some(d => d.name.text === "deserializable")
    ) {
      const name = s.name.text
      const marshalCode = buildMarshal(name, deserializableClasses[name])
      console.log(marshalCode)
      const marshalStmt = parseStatements(entrySrc, marshalCode)[0]
      const method = Node.createMethodDeclaration(
        marshalStmt.name,
        null,
        marshalStmt.signature,
        marshalStmt.body,
        null,
        CommonFlags.STATIC,
        entrySrc.range  // TODO
      )
      // s.members.push(method)
      entrySrc.statements.push(marshalStmt)
    }
  })
github pikapkg / builders / packages / plugin-wasm-assemblyscript / pkg / dist-src / index.js View on Github external
await new Promise((resolve, reject) => {
    asc.main(["src/index.ts", "--binaryFile", relativeOutWasm, "-d", relativeOutTypes, "--optimize", "--sourceMap", // Optional:
    "--use", " Math=JSMath", "-O3", "--importMemory", ...(options.args || [])], {
      stdout: reporter.stdout,
      stderr: reporter.stderr
    }, err => {
      if (err) {
        reject(err);
        return;
      }

      resolve();
    });
  });
  reporter.created(path.join(out, "assets/index.wasm"));
github graphprotocol / graph-cli / src / compiler.js View on Github external
)

      let outputFile = path.relative(baseDir, outFile)

      // Create output directory
      try {
        fs.mkdirsSync(path.dirname(outputFile))
      } catch (e) {
        throw e
      }

      let libs = path.join(baseDir, 'node_modules')
      let global = path.join(libs, '@graphprotocol', 'graph-ts', 'global', 'global.ts')
      global = path.relative(baseDir, global)

      asc.main(
        [
          inputFile,
          global,
          '--baseDir',
          baseDir,
          '--lib',
          libs,
          '--outFile',
          outputFile,
          '--optimize',
        ],
        {
          stdout: process.stdout,
          stderr: process.stdout,
        },
        e => {
github SinaMFE / assemblyscript-typescript-loader / src / index.js View on Github external
targetPath = path.join(
        buildTempPath,
        path.parse(this.resourcePath).name + ".wasm"
    );
    mkDirsSync(buildTempPath);
    let params = [
        path.relative(process.cwd(), this.resourcePath),
        "-o",
        path.relative(process.cwd(), targetPath)
        // ,
        // "--optimize",
        // "--validate",
        // "--sourceMap"
    ];
    optionUtils(params,options);
    asc.main(
        params,
        function(err) {
            if (err) throw err;
            var distStates = fs.statSync(targetPath);
            var distFile = fs.readFileSync(targetPath);
            // Options `dataUrlLimit` is backward compatibility with first loader versions
            var limit =
                options.limit ||
                (me.options && me.options.url && me.options.url.dataUrlLimit);
            if (limit) {
                limit = parseInt(limit, 10);
            }
            // var mimetype = options.mimetype || options.minetype || mime.lookup(this.resourcePath)
            if (!limit || distStates.size < limit) {
                me.addDependency(wasmFooterPath);
                var jsModule = transpile2Js(source);
github MichaelJCole / n-body-wasm-canvas / gulpfile.js View on Github external
gulp.task("build-assembly-asc", callback => {
  const asc = require("assemblyscript/bin/asc");
  asc.main([
    "nBodyForces.ts",
    "--baseDir", "src/assembly",
    // Output all the goodies asc has to offer
    "--binaryFile", "../../dist/assembly/nBodyForces.wasm",
    "--textFile", "../../dist/assembly/nBodyForces.wat",
    "--asmjsFile", "../../dist/assembly/nBodyForces.asc.js",
    "--idlFile", "../../dist/assembly/nBodyForces.webidl",
    "--tsdFile", "../../dist/assembly/nBodyForces.d.ts",
    "--sourceMap",
    // Configure compilation
//    "--runtime", "full",  // default
//    "-O3",        // https://github.com/AssemblyScript/assemblyscript/issues/838
//    "--noAssert",
    // Output timing info and validate
    "--measure",
    "--validate"
github SinaMFE / assemblyscript-typescript-loader / lib / index.js View on Github external
function AssemblyScriptLoader(source) {
  var options4me = loaderUtils.getOptions(this) || {};
  var innerCallback = this.async();
  validateOptions(require("./options"), options4me, "URL Loader");
  if (this.cacheable) {
    this.cacheable();
  }
  var me = this;
  var targetPath = this._compiler.outputPath;
  var buildTempPath = path.join(this._compiler.context,"/temp/assembly/");
  targetPath = path.join(
    buildTempPath, path.parse(this.resourcePath).name + ".wasm"
  );
  mkdirsSync(buildTempPath);
  asc.main(
    [
      path.relative(process.cwd(), this.resourcePath),
      // "./src/view/index/ass/test.ts",
      "-o",
      path.relative(process.cwd(), targetPath),
      "--optimize",
      "--validate",
      "--sourceMap"
    ],
    function(err) {
      if (err) throw err;
      var distStates = fs.statSync(targetPath);
      var distFile = fs.readFileSync(targetPath);
      // Options `dataUrlLimit` is backward compatibility with first loader versions
      var limit =
        options4me.limit ||
github MichaelJCole / n-body-wasm-canvas / src / workerWasm.js View on Github external
this.onmessage = function (evt) {

  // message from UI thread
  var msg = evt.data 
  switch (msg.purpose) {

    // Message: Load new wasm module

    case 'wasmModule': 
      // Instantiate the compiled module we were passed.
      wasm = loader.instantiate(msg.wasmModule, importObj)  // Throws
      // Tell nBodySimulation.js we are ready
      this.postMessage({ purpose: 'wasmReady' })
      return 


    // Message: Given array of floats describing a system of bodies (x,y,x,mass), 
    // calculate the Grav forces to be applied to each body

    case 'nBodyForces':
      if (!wasm) throw new Error('wasm not initialized')

      // Copy msg.arrBodies array into the wasm instance, increase GC count
      const dataRef = wasm.__retain(wasm.__allocArray(wasm.FLOAT64ARRAY_ID, msg.arrBodies));
      // Do the calculations in this thread synchronously
      const resultRef = wasm.nBodyForces(dataRef);
      // Copy result array from the wasm instance to our javascript runtime
github AssemblyScript / node / tests / node.js View on Github external
const { TestContext, EmptyReporter } = require("@as-pect/core");
const { instantiateBuffer } = require("assemblyscript/lib/loader");
const glob = require("glob");
const { main } = require("assemblyscript/cli/asc");
const { parse } = require("assemblyscript/cli/util/options");
const path = require("path");
const fs = require("fs");
const Wasi = require("wasi");
const wasi = new Wasi({});
const diff = require("diff");

const options = parse(process.argv.slice(2), {
  "help": {
    "description": "Prints this message and exits.",
    "type": "b",
    "alias": "h"
  },
  "updateFixtures": {
    "description": "Update the test fixtures.",
    "type": "b",
    "alias": "u"
  },
});

if (options.unknown.length > 1) {
  console.error("Unknown options arguments: " + options.unknown.join(" "));
  process.exit(1);
}