Skip to content

Instantly share code, notes, and snippets.

@valstu
Last active August 17, 2022 17:08
Show Gist options
  • Save valstu/9a7877dee0010e65afcc963646fb4b4a to your computer and use it in GitHub Desktop.
Save valstu/9a7877dee0010e65afcc963646fb4b4a to your computer and use it in GitHub Desktop.

Revisions

  1. valstu revised this gist Aug 17, 2022. 2 changed files with 14 additions and 9 deletions.
    17 changes: 11 additions & 6 deletions HooksLib.ts
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,23 @@
    @external("env", "_g")
    export declare function GUARD(id: i32, maxiter: i32): i64
    export declare function _g(id: i32, maxiter: i32): i64

    @external("env", "accept")
    export declare function ACCEPT(read_ptr: string, read_len: i32, err: i64): i64
    export declare function accept(read_ptr: string, read_len: i32, err: i64): i64

    @external("env", "trace")
    export declare function TRACE(mread_ptr: string, mread_len: i32, dread_ptr: string, dread_len: i32, as_hex: i32): i64

    ////////////////////////

    export function LOG (text: string): void {
    function LOG (text: string): void {
    TRACE(text, text.length * 2, text, text.length * 2, 0)
    }

    export function OK (text: string = 'OK'): void {
    ACCEPT(text, text.length * 2, 0)
    }
    export function hook(reserved: i32 = 0): i64 {
    LOG("Accept.c: Called.");
    accept('', 0, 0);

    _g(1,1); // every hook needs to import guard function and use it at least once
    // unreachable
    return 0;
    }
    6 changes: 3 additions & 3 deletions module.ts
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    import {GUARD, LOG, OK} from './HooksLib'

    //export function cbak(reserved: i32 = 0): i64 {
    // return 0
    // }
    export function cbak(reserved: i32 = 0): i64 {
    return 0
    }

    export function hook(reserved: i32 = 0): i64 {
    GUARD(1, 1)
  2. valstu created this gist Aug 17, 2022.
    18 changes: 18 additions & 0 deletions HooksLib.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    @external("env", "_g")
    export declare function GUARD(id: i32, maxiter: i32): i64

    @external("env", "accept")
    export declare function ACCEPT(read_ptr: string, read_len: i32, err: i64): i64

    @external("env", "trace")
    export declare function TRACE(mread_ptr: string, mread_len: i32, dread_ptr: string, dread_len: i32, as_hex: i32): i64

    ////////////////////////

    export function LOG (text: string): void {
    TRACE(text, text.length * 2, text, text.length * 2, 0)
    }

    export function OK (text: string = 'OK'): void {
    ACCEPT(text, text.length * 2, 0)
    }
    17 changes: 17 additions & 0 deletions module.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    import {GUARD, LOG, OK} from './HooksLib'

    //export function cbak(reserved: i32 = 0): i64 {
    // return 0
    // }

    export function hook(reserved: i32 = 0): i64 {
    GUARD(1, 1)

    for (let i = 0; GUARD(10, 10), i < 3; i++) {
    LOG('-- :D --')
    }

    OK('-- :) --')

    return 0
    }