(module (import "js" "tbl" (table $tbl 4 anyfunc)) ;; import increment function -- the JS function (import "js" "increment" (func $increment (result i32))) ;; import decrement function -- ths JS function (import "js" "decrement" (func $decrement (result i32))) ;; import wasm_increment (import "js" "wasm_increment" (func $wasm_increment (result i32))) ;; import wasm_decrement (import "js" "wasm_decrement" (func $wasm_decrement (result i32))) ;; table function type definitions all i32 and take no parameters (type $returns_i32 (func (result i32))) ;; JS increment function in table at index 0 (global $inc_ptr i32 (i32.const 0)) ;; JS decrement function in table at index 1 (global $dec_ptr i32 (i32.const 1)) ;; wasm increment function in table at index 2 (global $wasm_inc_ptr i32 (i32.const 2)) ;; wasm decrement function in table at index 3 (global $wasm_dec_ptr i32 (i32.const 3)) ;; Test performance of indirect table call of JS func (func (export "js_table_test") (loop $inc_cycle ;; indirect call to JS increment func (call_indirect (type $returns_i32) (global.get $inc_ptr)) i32.const 4_000_000 ;; is the value returned by call to $inc_ptr <= 4_000_000? i32.le_u br_if $inc_cycle ;; if so loop again ) (loop $dec_cycle ;; indirect call to JS increment func (call_indirect (type $returns_i32) (global.get $dec_ptr)) i32.const 4_000_000 ;; is the value returned by call to $inc_ptr <= 4_000_000? i32.le_u br_if $dec_cycle ;; if so loop again ) ) ;; Test performance of direct call to JS function (func (export "js_import_test") (loop $inc_cycle call $increment i32.const 4_000_000 i32.le_u br_if $inc_cycle ) (loop $dec_cycle call $decrement i32.const 4_000_000 i32.le_u br_if $dec_cycle ) ) ;; Test performance of an indirect call to WASM (func (export "wasm_table_test") (loop $inc_cycle ;; indirect call to WASM increment function (call_indirect (type $returns_i32) (global.get $wasm_inc_ptr)) i32.const 4_000_000 i32.le_u br_if $inc_cycle ) (loop $dec_cycle ;; indirect call to WASM increment function (call_indirect (type $returns_i32) (global.get $wasm_dec_ptr)) i32.const 4_000_000 i32.le_u br_if $dec_cycle ) ) (func (export "wasm_import_test") (loop $inc_cycle ;; direct call to WASM inc func call $wasm_increment i32.const 4_000_000 i32.le_u br_if $inc_cycle ) (loop $dec_cycle ;; direct call to WASM inc func call $wasm_decrement i32.const 4_000_000 i32.le_u br_if $dec_cycle ) ) ) ;; end of module