How to use the @babel/template.default.program function in @babel/template

To help you get started, we’ve selected a few @babel/template 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 xtuc / webassemblyjs / packages / wasm-text-gen / src / printers / javascript.js View on Github external
const { traverse } = require("@webassemblyjs/ast");

const template = require("@babel/template").default;
const generate = require("@babel/generator").default;
const t = require("@babel/types");

const globalInstanceIdentifier = t.identifier("instance");
const globalMemoryIdentifier = t.identifier("_memory0");
const globalTableIdentifier = t.identifier("_table0");

const exportFuncTemplate = template.program(`
  export function NAME(ARGS) {
    if (typeof INSTANCE === "undefined") {
      throw new Error("Can not call function " + NAME.name + ", module not initialized.");
    }

    return INSTANCE.exports.NAME(ARGS);
  }
`);

const headerTemplate = template.program(`
  if (typeof WebAssembly === "undefined") {
    throw new Error("WebAssembly not supported");
  }

  let INSTANCE;
github xtuc / webassemblyjs / packages / wasm-text-gen / src / printers / javascript.js View on Github external
const globalInstanceIdentifier = t.identifier("instance");
const globalMemoryIdentifier = t.identifier("_memory0");
const globalTableIdentifier = t.identifier("_table0");

const exportFuncTemplate = template.program(`
  export function NAME(ARGS) {
    if (typeof INSTANCE === "undefined") {
      throw new Error("Can not call function " + NAME.name + ", module not initialized.");
    }

    return INSTANCE.exports.NAME(ARGS);
  }
`);

const headerTemplate = template.program(`
  if (typeof WebAssembly === "undefined") {
    throw new Error("WebAssembly not supported");
  }

  let INSTANCE;

  const MEMORY = new WebAssembly.Memory({initial: 100, limit: 1000});
  const TABLE = new WebAssembly.Table({initial: 0, element: 'anyfunc'});
`);

const initFuncTemplate = template.program(`
  export const memory = MEMORY;
  export const table = TABLE;

  export default function(opts = {env:{}}) {
github xtuc / webassemblyjs / packages / wasm-text-gen / src / printers / javascript.js View on Github external
return INSTANCE.exports.NAME(ARGS);
  }
`);

const headerTemplate = template.program(`
  if (typeof WebAssembly === "undefined") {
    throw new Error("WebAssembly not supported");
  }

  let INSTANCE;

  const MEMORY = new WebAssembly.Memory({initial: 100, limit: 1000});
  const TABLE = new WebAssembly.Table({initial: 0, element: 'anyfunc'});
`);

const initFuncTemplate = template.program(`
  export const memory = MEMORY;
  export const table = TABLE;

  export default function(opts = {env:{}}) {

    if (typeof opts.env.memory === "undefined") {
      opts.env.memory = MEMORY;
    }

    if (typeof opts.env.table === "undefined") {
      opts.env.table = TABLE;
    }

    const importObject = opts;

    const getArrayBuffer = response => response.arrayBuffer();