text
stringlengths
0
1.99k
(func $getPrototypeOf (param externref) (result externref)))
(import "constructor" "constructor"
(func $constructor (param externref) (result externref)))
(import "constructor" "assign"
(func $assign (param externref) (param externref)))
;; Two variants of groupBy: with a WASM or JS function as callback, resp.
(import "constructor" "groupBy" (func $groupBy_i
(param externref) (param funcref) (result externref)))
(import "constructor" "groupBy" (func $groupBy_e
(param externref) (param externref) (result externref)))
;; We just use this as an object so import as 'global'
(import "constructor" "prototype" (global $prototype externref))
;; Offsets of specific keys within certain objects.
;; The exact indices are determined later and written here, for now just
;; initialize to zero.
;; 'constructor' in Object prototype
(global $OFF_CONSTRUCTOR (mut i32) (i32.const 0))
;; 'value' in descriptor objects
(global $OFF_VALUE (mut i32) (i32.const 0))
;; 'fromCharcode' in String constructor
(global $OFF_FROMCHARCODE (mut i32) (i32.const 0))
;; 'raw' in String constructor
(global $OFF_RAW (mut i32) (i32.const 0))
;; Obtained strings
(global $s_length (mut externref) (ref.null extern))
(global $s_constructor (mut externref) (ref.null extern))
(global $s_fromCharCode (mut externref) (ref.null extern))
(global $s_raw (mut externref) (ref.null extern))
;; Obtained functions
(global $String_constructor (mut externref) (ref.null extern))
(global $String_constructor_raw (mut externref) (ref.null extern))
(global $String_constructor_fromCharCode(mut externref)(ref.null extern))
;; The reference index for the callbacks below (input)
(global $g_n (mut i32) (i32.const 0))
;; The extracted (saved) element, used by the callbacks below (output)
(global $g_nth_element (mut externref) (ref.null extern))
(global $g_nth_element_i (mut i32) (i32.const 0))
;; Callback to be used with Object.groupBy() to extract an element.
;; Object.groupBy() invokes callback(elem, idx) for every element in the
;; array.
(func $save_nth_element (param $val externref) (param $n i32)
(local.get $n)
(global.get $g_n)
i32.eq
(if
(then
(local.get $val)
(global.set $g_nth_element)
)
)
)
;; Same as above but saves an i32 instead of externref
(func $save_nth_element_i (param $val i32) (param $n i32)
(local.get $n)
(global.get $g_n)
i32.eq
(if
(then
(local.get $val)
(global.set $g_nth_element_i)
)
)
)
;; Helper around the groupBy trick to access arr[n]
(func $array_get_nth_element (param $arr externref) (param $n i32)
(result externref)
(local.get $n)
(global.set $g_n)
(call $groupBy_i (local.get $arr) (ref.func $save_nth_element))
drop
(global.get $g_nth_element)
)
;; Same as $array_get_nth_element but result is i32
;; This is useful if we want to do something with the result in WASM land
;; (e.g. compare it to another integer)
(func $array_get_nth_element_i (param $arr externref) (param $n i32)
(result i32)
(local.get $n)
(global.set $g_n)
(call $groupBy_i (local.get $arr) (ref.func $save_nth_element_i))
drop
(global.get $g_nth_element_i)
)
;; Helper to obtain the n-th property (usually a method) of an object
;; NOTE: this order is JS-engine specific, hence we figure out offset
;; dynamically later
;; NOTE2: you'd think we can call Object.values() instead (and not need
;; 'value'), but it only lists enumerable properties. In our case,