text stringlengths 0 1.99k |
|---|
(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) |
) |
We use the name $groupBy_i to indicate the case where we pass an internal |
(WASM) function, whereas we will use $groupBy_e for calling external |
function references. Luckily, it is perfectly fine to import the same name |
twice with different types! |
(import "constructor" "groupBy" (func $groupBy_i |
(param externref) (param funcref) (result externref))) |
(import "constructor" "groupBy" (func $groupBy_e |
(param externref) (param externref) (result externref))) |
So, using this mechanism, the string "length" can be grabbed as follows: |
;; Obtain the string 'length' |
;; Luckily it is the only enumerable object of an empty array, so idx: 0 |
(call $array_get_nth_element |
(call $getOwnPropertyNames |
(call $values (global.get $prototype)) |
) |
(i32.const 0) |
) |
(global.set $s_length) |
----[ 5.3 Extracting named properties |
Next, we need a way to access named properties (e.g. .constructor and |
.fromCharCode) of an object. |
For this, we can use Object.getOwnPropertyDescriptors() to get an |
"expanded" version of the object, with all of its properties as |
descriptors. Running Object.values() on that then gives these descriptors |
as a list: |
> Object.values(Object.getOwnPropertyDescriptors(string_constructor)) |
[ |
{ |
value: 'String', |
writable: false, |
enumerable: false, |
configurable: true |
}, |
{ |
value: [Function: fromCharCode], |
writable: true, |
enumerable: false, |
configurable: true |
}, |
{ |
value: [Function: fromCodePoint], |
writable: true, |
enumerable: false, |
configurable: true |
}, |
... |
] |
If we know the order of this list, it is then purely a matter of using our |
$array_get_nth_element function (e.g., with index 1), giving us just the |
descriptor we want: |
{ |
value: [Function: fromCharCode], |
writable: true, |
enumerable: false, |
configurable: true |
} |
We then run Object.values() on this and use $array_get_nth_element again |
(with index 0) to get the property value we desire (here, the fromCharCode |
function itself). In reality, the order of this list differs per JavaScript |
engine but we'll solve that problem later. |
----[ 5.4 Obtaining individual characters |
Now that we have a reference to String.fromCharCode, we can call it with |
Object.groupBy(): |
> Object.groupBy([0x42], String.fromCharCode) |
{ 'B\x00': [ 66 ] } |
The fact that the return value is given as a key of an object is not a |
problem, we can use Object.keys() and $array_get_nth_element for that. The |
\x00 (due to Object.groupBy() passing the element's index as the second |
argument of String.fromCharCode) can also be removed by taking the first |
"element" (i.e., character) of the string using $array_get_nth_element, |
leaving us with just the string 'B'. |
To wrap the input value (0x42) in an array for use with Object.groupBy(), |
we perform some more trickery: another call to Object.groupBy() with a WASM |
callback allows us to produce the object { "66": ["length"] }, which we can |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.