Class SpiderExpression

A series of instructions which can be executed at runtime.

Hierarchy

  • SpiderExpression

Constructors

Properties

instructions: SpiderInstruction<any[]>[]

The actual list of instructions.

Methods

  • Appends an instruction at the end of this expression.

    Type Parameters

    • T extends any[]

    Parameters

    • opcode: SpiderOpcode<T>

      The type of instruction to append.

    • Rest ...args: T

      The arguments of this instruction. Changes depending upon the opcode.

    Returns void

  • Emits a block instruction with a new expression as the body.

    Parameters

    • Optional lambda: ((block) => any)

      A function invoked before this method returns which emits instructions to the expression inside the block.

    • Optional blocktype: SpiderValueType

      The type of value left on the stack after the emitted block has executed. Leave undefined for blocks which leave no value on the stack.

    Returns SpiderExpression

    The expression executed inside this block. This is the same expression passed to the lambda function.

    Example

    func.body.emitBlock(SpiderNumberType.f64, expr => {
    expr.emitConstant(SpiderNumberType.f64, 1);
    expr.emitConstant(SpiderNumberType.f64, 2);
    expr.emit(SpiderNumberType.f64_add);
    });
  • Emits one of i32_const, i64_const, f32_const or f64_const with the value depending on the type argument.

    Parameters

    • type: SpiderNumberType

      The type of const instruction to be emitted.

    • value: number | bigint

      The argument of the const instruction to be emitted.

    Returns void

    See

    emit

  • Emits an if instruction with a new expression as the body, without an else expression.

    Parameters

    • Optional lambda: ((block) => any)

      A function invoked before this method returns which emits instructions to the expression which executes if the condition is true.

    • Optional blocktype: SpiderValueType

      The type of value left on the stack if the condition is true. Leave undefined for expressions which leave no value on the stack.

    Returns SpiderExpression

    The expression executed inside the if statement. This is the same expression passed to the lambda function.

    Example

    An if statement which makes sure that 1 + 1 = 2.

    const loopCounter = func.addLocalVariable(SpiderNumberType.i32);
    func.body.emitConstant(SpiderNumberType.i32, 1);
    func.body.emitConstant(SpiderNumberType.i32, 1);
    func.body.emit(SpiderNumberType.i32_add); // 1 + 1
    func.body.emitConstant(SpiderNumberType.i32, 2);
    func.body.emit(SpiderNumberType.i32_eq); // 1 + 1 === 2
    func.body.emitIf(expr => {
    expr.emit(SpiderOpcodes.call, mathIsntBrokenYay);
    });
  • Emits an if instruction with an else block using two new expressions as the bodys.

    Parameters

    • Optional lambdaTrue: ((block) => any)

      A function invoked before this method returns which emits instructions to the expression which executes if the condition is true.

    • Optional lambdaFalse: ((block) => any)

      A function invoked before this method returns which emits instructions to the expression which executes if the condition is false.

    • Optional blocktype: SpiderValueType

      The type of value left on the stack after the emitted if or else expressions have executed. Leave undefined for expressions which leave no value on the stack.

    Returns {
        instrFalse: SpiderExpression;
        instrTrue: SpiderExpression;
    }

    The two expressions the could be executed.

    Example

    An if-else statement which makes sure that 1 + 1 = 2.

    const loopCounter = func.addLocalVariable(SpiderNumberType.i32);
    func.body.emitConstant(SpiderNumberType.i32, 1);
    func.body.emitConstant(SpiderNumberType.i32, 1);
    func.body.emit(SpiderNumberType.i32_add); // 1 + 1
    func.body.emitConstant(SpiderNumberType.i32, 2);
    func.body.emit(SpiderNumberType.i32_eq); // 1 + 1 === 2
    func.body.emitIfElse(expr => { // if
    expr.emit(SpiderOpcodes.call, mathIsntBrokenYay);
    }, expr => { // else
    expr.emit(SpiderOpcodes.call, mathIsBrokenOmfg);
    });
  • Emits a loop instruction with a new expression as the body.

    Parameters

    • Optional lambda: ((block) => any)

      A function invoked before this method returns which emits instructions to the expression inside the loop.

    • Optional blocktype: SpiderValueType

      The type of value left on the stack after the emitted loop has executed. Leave undefined for blocks which leave no value on the stack.

    Returns SpiderExpression

    The expression executed inside this block. This is the same expression passed to the lambda function.

    Example

    A loop which runs 10 times.

    const loopCounter = func.addLocalVariable(SpiderNumberType.i32);
    func.body.emitConstant(SpiderNumberType.i32, 0);
    func.body.emit(SpiderOpcodes.local_set, loopCounter);
    func.body.emitLoop(expr => {
    expr.emit(SpiderOpcodes.local_get, loopCounter);
    expr.emitConstant(SpiderNumberType.i32, 1);
    expr.emit(SpiderOpcodes.i32_add)
    expr.emit(SpiderOpcodes.local_tee, loopCounter);
    expr.emitConstant(SpiderNumberType.i32, 10);
    expr.emit(SpiderOpcodes.i32_ne);
    expr.emit(SpiderOpcodes.br_if, 0);
    });
  • If this expression has one instruction which pushes a funcref onto the stack, returns the function referred to by the constant funcref or null if the constant funcref is null.

    Returns null | SpiderFunction

    Throws

    If this expression doesn't have exactly one instruction, or that one instruction's opcode isn't global_get.

  • If this expression has one instruction which pushes the value of a global onto the stack, returns that global.

    Returns SpiderGlobal

    Throws

    If this expression doesn't have exactly one instruction, or that one instruction's opcode isn't global_get.

  • If this expression has one instruction which pushes a constant number onto the stack, returns that number.

    Returns number

    Example

    Gets the number the global is being set to on module initialization.

    const globalValue = global.value.getAsConstNumber();
    

    Throws

    If this expression doesn't have exactly one instruction, or that one instruction's opcode isn't a constant number.

  • Clears this expression and emits the instruction to push a constant with the specified type and value onto the stack.

    Parameters

    Returns void

    Example

    global.value.setToConst(SpiderNumberType.i64, 69420);
    

    Throws

    TypeError if type and value are incompatible.

  • Clears this expression and emits the instruction to push a reference to the function or a null function reference onto the stack.

    Parameters

    Returns void

  • Clears this expression and emits the instruction to push the value of a global onto the stack.

    Parameters

    Returns void

  • Clears this expression and emits the instruction to push a number with the specified type and value onto the stack.

    Parameters

    Returns void

Generated using TypeDoc