How to use mamacro - 10 common examples

To help you get started, we’ve selected a few mamacro 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 / webassemblyjs / src / interpreter / kernel / exec.js View on Github external
const index = frame.values.slice(0).reverse().findIndex(({type}) => type === "label");
    // some expression like inittializer don't have labels currently, so this is
    // guarantee to fail
    // assertRuntimeError(index !== -1, "POP_LABEL: label not found")

    if (index !== -1) {
      const initialOrderIndex = frame.values.length - 1 - index;

      trace("exiting block " + frame.values[initialOrderIndex].value);

      frame.values.splice(initialOrderIndex, 1);
    }
  `
);

define(
  GOTO,
  labelOffset => `
    pc = offsets.indexOf(String(${labelOffset}));
  `
);

define(
  RETURN,
  () => `
    const activeFrame = getActiveStackFrame();

    if (activeFrame.values.length > 0) {
      return pop1(activeFrame);
    } else {
      return;
    }
github xtuc / webassemblyjs / packages / webassemblyjs / src / interpreter / kernel / exec.js View on Github external
);

define(
  RETURN,
  () => `
    const activeFrame = getActiveStackFrame();

    if (activeFrame.values.length > 0) {
      return pop1(activeFrame);
    } else {
      return;
    }
  `
);

define(
  PUSH_NEW_STACK_FRAME,
  pc => `
    const stackframe = require("./stackframe");

    const activeFrame = getActiveStackFrame();
    const newStackFrame = stackframe.createChildStackFrame(activeFrame, ${pc});

    // move active frame
    framepointer++;

    if (framepointer >= 300) {
      throw new RuntimeError("Maximum call stack depth reached");
    }

    // Push the frame on top of the stack
    callStack[framepointer] = newStackFrame;
github xtuc / webassemblyjs / packages / wast-parser / src / tokenizer.js View on Github external
// @flow

import { define } from "mamacro";
import { FSM, makeTransition } from "@webassemblyjs/helper-fsm";
import { codeFrameFromSource } from "@webassemblyjs/helper-code-frame";

declare function unexpectedCharacter(): void;

/**
 * Throw an error in case the current character is invalid
 */
define(unexpectedCharacter, () =>
  `throw new Error(getCodeFrame(input, line, column) + "Unexpected character " + JSON.stringify(char));`);

// eslint-disable-next-line
function getCodeFrame(source: string, line: number, column: number) {
  const loc = {
    start: { line, column }
  };

  return "\n" + codeFrameFromSource(source, loc) + "\n";
}

const WHITESPACE = /\s/;
const PARENS = /\(|\)/;
const LETTERS = /[a-z0-9_/]/i;
const idchar = /[a-z0-9!#$%&*+./:<=>?@\\[\]^_`|~-]/i;
const valtypes = ["i32", "i64", "f32", "f64"];
github xtuc / webassemblyjs / packages / webassemblyjs / src / interpreter / kernel / exec.js View on Github external
define(GOTO, labelOffset => `
    pc = offsets.indexOf(String(${labelOffset}));
  `);

define(RETURN, () => `
    const activeFrame = getActiveStackFrame();

    if (activeFrame.values.length > 0) {
      return pop1(activeFrame);
    } else {
      return;
    }
  `);

define(PUSH_NEW_STACK_FRAME, pc => `
    const stackframe = require("./stackframe");

    const activeFrame = getActiveStackFrame();
    const newStackFrame = stackframe.createChildStackFrame(activeFrame, ${pc});

    // move active frame
    framepointer++;

    if (framepointer >= 300) {
      throw new RuntimeError("Maximum call stack depth reached");
    }

    // Push the frame on top of the stack
    callStack[framepointer] = newStackFrame;
  `);
github xtuc / webassemblyjs / packages / webassemblyjs / src / interpreter / kernel / exec.js View on Github external
const index = frame.values.slice(0).reverse().findIndex(({type}) => type === "label");
    // some expression like inittializer don't have labels currently, so this is
    // guarantee to fail
    // assertRuntimeError(index !== -1, "POP_LABEL: label not found")

    if (index !== -1) {
      const initialOrderIndex = frame.values.length - 1 - index;

      trace("exiting block " + frame.values[initialOrderIndex].value);

      frame.values.splice(initialOrderIndex, 1);
    }
  `);

define(GOTO, labelOffset => `
    pc = offsets.indexOf(String(${labelOffset}));
  `);

define(RETURN, () => `
    const activeFrame = getActiveStackFrame();

    if (activeFrame.values.length > 0) {
      return pop1(activeFrame);
    } else {
      return;
    }
  `);

define(PUSH_NEW_STACK_FRAME, pc => `
    const stackframe = require("./stackframe");
github xtuc / webassemblyjs / packages / webassemblyjs / src / interpreter / kernel / exec.js View on Github external
export function executeStackFrame(
  { program }: IR,
  offset: number,
  firstFrame: StackFrame
): ?StackLocal {
  assertRuntimeError(typeof program === "object");

  const callStack: Array = [firstFrame];

  // because it's used a macros
  // eslint-disable-next-line prefer-const
  let framepointer = 0;

  function getLocalByIndex(frame: StackFrame, index: number) {
    const local = frame.locals[index];

    if (typeof local === "undefined") {
      throw newRuntimeError(
        "Assertion error: no local value at index " + index
      );
    }
github xtuc / webassemblyjs / packages / webassemblyjs / src / interpreter / kernel / exec.js View on Github external
const subroutine = frame.allocator.get(funcaddr);

        if (typeof subroutine !== "object") {
          throw newRuntimeError(
            `Cannot call function at address ${funcaddr}: not a function`
          );
        }

        // 4. Invoke the function instance at address a

        // FIXME(sven): assert that res has type of resultType
        const [argTypes, resultType] = subroutine.type;

        const args = popArrayOfValTypes(frame, argTypes);

        assertRuntimeError(subroutine.isExternal);

        const res = subroutine.code(args.map(arg => arg.value));

        if (typeof res !== "undefined") {
          pushResult(frame, castIntoStackLocalOfType(resultType, res));
        }

        break;
      }
    }

    switch (instruction.id) {
      case "const": {
        // https://webassembly.github.io/spec/core/exec/instructions.html#exec-const

        // $FlowIgnore
github xtuc / webassemblyjs / packages / webassemblyjs / src / interpreter / kernel / exec.js View on Github external
assertRuntimeError(framepointer > -1, "call stack underflow");

    const frame = callStack[framepointer];
    assertRuntimeError(frame !== undefined, "no frame at " + framepointer);

    return frame;
  }

  const offsets = Object.keys(program);
  let pc = offsets.indexOf(String(offset));

  while (true) {
    const frame = getActiveStackFrame();
    const instruction = program[parseInt(offsets[pc])];

    assertRuntimeError(
      instruction !== undefined,
      `no instruction at pc ${pc} in frame ${framepointer}`
    );

    // $FlowIgnore
    trace(`exec ${instruction.type}(${instruction.id || ""})`);

    if (typeof frame.trace === "function") {
      frame.trace(framepointer, pc, instruction, frame);
    }

    pc++;

    switch (instruction.type) {
      case "InternalEndAndReturn": {
        if (frame.returnAddress !== -1) {
github xtuc / webassemblyjs / packages / webassemblyjs / src / interpreter / kernel / exec.js View on Github external
import { assertRuntimeError, define } from "mamacro";

import Long from "long";
import { Memory } from "../runtime/values/memory";
import { RuntimeError } from "../../errors";

declare function trace(msg?: string): void;
declare function GOTO(l: number): void;
declare function RETURN(): void;
declare function PUSH_NEW_STACK_FRAME(pc: number): void;
declare function POP_STACK_FRAME(): void;
declare function POP_LABEL(): void;
declare function assertNItemsOnStack(n: number): void;

define(
  assertNItemsOnStack,
  n => `
  if (frame.values.length < ${n}) {
    throw new RuntimeError(
      "Assertion error: expected " + JSON.stringify(${n})
        + " on the stack, found " + frame.values.length
    );
  }`
);

define(
  trace,
  msg => `
    console.log("trace " + ${msg});
  `
);
github xtuc / webassemblyjs / packages / ast / src / nodes.js View on Github external
export function producerMetadata(
  language: Array,
  processedBy: Array,
  sdk: Array
): ProducerMetadata {
  assert(
    typeof language === "object" && typeof language.length !== "undefined"
  );

  assert(
    typeof processedBy === "object" && typeof processedBy.length !== "undefined"
  );

  assert(typeof sdk === "object" && typeof sdk.length !== "undefined");

  const node: ProducerMetadata = {
    type: "ProducerMetadata",
    language,
    processedBy,
    sdk
  };

mamacro

MIT
Latest version published 6 years ago

Package Health Score

62 / 100
Full package analysis