text
stringlengths
0
1.99k
;; to $obj, allowing us to accumulate characters to convert to a string
;; later. We use Object.groupBy under the hood, which wraps values in
;; lists. So we're constructing something like [['f'],['o'],['o']].
;; Luckily this doesn't matter later on because the String.raw behavior.
(func $add_charcode_to_obj (param $obj externref) (param $c i32)
(call $add_value_to_obj
(local.get $obj)
(call $chr (local.get $c))
)
)
;; Turns a list such as [["a"],["b"],["c"],["d"]] into ["a0bcd"]
;; (yes, the "0" is added after the first character, due to how we use
;; Object.groupBy) (yes, it returns an array, but this is fine as the
;; implicit .toString gives the string)
(func $list_to_string (param $list externref) (result externref)
(local $arrwithobj externref)
(local $innerobj externref)
(local $res externref)
;; Create an object with the key "raw", with as value our input list
;; because String.raw uses this key to build the string. (groupBy wraps
;; grouped elements in a list, but they all group to the same key so it
;; gives back the original list)
(global.get $s_raw)
(global.set $g_val_e)
(call $groupBy_i (local.get $list) (ref.func $return_val_e))
(local.set $res)
;; This is just a way to get an array with a single object. We need
;; this as we want to give the object as a parameter to String.raw(),
;; but it needs to be wrapped in an array for the Object.groupBy trick.
(call $values (call $getOwnPropertyDescriptors (local.get $res)))
(local.set $arrwithobj)
;; Grab the inner object, for the purpose of modifying it
(call $array_get_nth_element (local.get $arrwithobj) (i32.const 0))
(local.set $innerobj)
;; Merge 'res' into it (our object with the raw key)
(call $assign (local.get $innerobj) (local.get $res))
;; Now we can invoke String.raw via Object.groupBy
(call $groupBy_e
(local.get $arrwithobj) (global.get $String_constructor_raw)
)
;; The result is in the key of the returned object, so extract that
(call $keys)
)
;; Globals used by $array_index_of and $save_idx_if_length_equals
(global $g_wanted_elem (mut i32) (i32.const 999))
(global $g_wanted_length (mut i32) (i32.const 0))
(global $g_obtained_idx (mut i32) (i32.const 999))
;; Callback to be used with func $array_index_of
(func $save_idx_if_equals (param $elem i32) (param $idx i32)
(local.get $elem)
(global.get $g_wanted_elem)
i32.eq
;; if($elem == $g_wanted_elem) $g_obtained_idx = idx;
(if
(then
(local.get $idx)
(global.set $g_obtained_idx)
)
)
)
;; Callback to be used with func $get_index_for_element_with_length
(func $save_idx_if_length_equals (param $elem externref) (param $idx i32)
;; Get the descriptor of the 'length' prop, then get its 'value'
(call $array_get_nth_element_i
(call $values
(call $getOwnPropertyDescriptor
(local.get $elem) (global.get $s_length)
)
)
(global.get $OFF_VALUE) ;; The index of 'value' in prop descriptors
)
;; if(len == $g_wanted_length) $g_obtained_idx = idx;
(global.get $g_wanted_length)
i32.eq
(if
(then
(local.get $idx)
(global.set $g_obtained_idx)
)
)
)
;; Get the index of the $needle in $arr. Used later to obtain OFF_VALUE
;; (in property descriptor)
(func $array_index_of (param $arr externref) (param $needle i32)
(result i32)
(local.get $needle)
(global.set $g_wanted_elem)