text
stringlengths
0
1.99k
;; we would like to access any property.
(func $object_get_nth_property (param $obj externref) (param $n i32)
(result externref)
(local $descriptor_vals externref) ;; for readability
(call $values (call $getOwnPropertyDescriptors (local.get $obj)))
(local.set $descriptor_vals)
(call $values
(call $array_get_nth_element
(local.get $descriptor_vals)
(local.get $n)
)
)
(global.get $OFF_VALUE)
(call $array_get_nth_element)
)
(func $object_get_named_property (param $obj externref)
(param $key externref)
(result externref)
(call $getOwnPropertyDescriptor (local.get $obj) (local.get $key))
call $values
(global.get $OFF_VALUE)
call $array_get_nth_element
)
;; Callbacks that return a fixed (but configurable) value, i.e. ()=>42
;; We need versions with JS objects and with an int. These are used
;; together with Object.groupBy() to be able to set an arbitrary key on
;; the resulting object.
(global $g_val_e (mut externref) (ref.null extern))
(func $return_val_e (result externref)
(global.get $g_val_e)
)
(global $g_val_i (mut i32) (i32.const 0))
(func $return_val_i (result i32)
(global.get $g_val_i)
)
;; Callback that returns a static (incrementing) value each time.
;; Used by $add_value_to_obj.
(global $g_key_ctr (mut i32) (i32.const 0))
(func $return_incr_ctr (result i32)
(global.get $g_key_ctr)
(i32.const 1)
i32.add ;; increment
(global.set $g_key_ctr)
(global.get $g_key_ctr) ;; return
)
;; Adds $value to obj, under a new unique (incrementing) key
;; This is so we can accumulate values with unique incrementing keys on
;; an object, used to build an array later.
(func $add_value_to_obj (param $obj externref) (param $value externref)
(local $tmp externref)
;; Create a new object ($tmp) which has just a single property with a
;; controlled value ($value) and a unique key (the return value of the
;; callback tells Object.groupBy what property name to use)
(call $groupBy_i (local.get $value) (ref.func $return_incr_ctr))
(local.set $tmp)
;; Use Object.assign() to add the property to $obj
(call $assign (local.get $obj) (local.get $tmp))
)
;; A convoluted way to call fromCharCode on a single number.
(func $chr (param $c i32) (result externref)
(local $tmp externref)
;; This is just a way to get an array with one element,
;; so groupBy invokes the callback just once.
(call $getOwnPropertyNames (call $values (global.get $prototype)))
(local.set $tmp) ;; [ 'length' ]
;; First we call Object.groupBy() on a single-element array, with a
;; callback that returns a fixed value ($c), to create an object with
;; just that key. For example, for 65 we'd obtain { "65": ["length"] }
(local.get $c)
(global.set $g_val_i)
(call $groupBy_i (local.get $tmp) (ref.func $return_val_i))
;; Then, we call Object.fromCharCode on it by passing e.g. [ "65" ] to
;; Object.groupBy(). (Object.keys will wrap it in an array for us)
(call $groupBy_e
(call $keys)
(global.get $String_constructor_fromCharCode)
)
;; Now Object.keys() gives ['A\x00'], so we do _[0][0] to get just 'A'
(call $keys)
(i32.const 0)
(call $array_get_nth_element)
(i32.const 0)
(call $array_get_nth_element)
)
;; Basically adds String.fromCharCode($c) as a new value with unique key