text stringlengths 0 1.99k |
|---|
constructor and all of the static methods on the Object constructor (a.k.a. |
the Object global): |
Object.assign Object.create |
Object.defineProperties Object.defineProperty |
Object.entries Object.freeze |
Object.fromEntries Object.getOwnPropertyDescriptor |
Object.getOwnPropertyDescriptors Object.getOwnPropertyNames |
Object.getOwnPropertySymbols Object.getPrototypeOf |
Object.groupBy Object.hasOwn |
Object.is Object.isExtensible |
Object.isFrozen Object.isSealed |
Object.keys Object.length |
Object.name Object.preventExtensions |
Object.prototype Object.seal |
Object.setPrototypeOf Object.values |
----[ 5.1 Dynamic function calling |
At first these all seem relatively boring, but an unexpected hero here is |
Object.groupBy() [2]: |
Object.groupBy(items, callbackFn) |
"The Object.groupBy() static method groups the elements of a given |
iterable according to the string values returned by a provided callback |
function. The returned object has separate properties for each group, |
containing arrays with the elements in the group." |
This relatively new addition to the JavaScript language does a bunch of |
things we'll end up needing. Most importantly it will call a function for |
us, solving the second problem from before. |
For example, if we somehow manage to obtain a useful Function instance, we |
can call it like this to run the JavaScript code: |
x = Function("alert('hello world')"); // assuming we have this string |
Object.groupBy([1],x) // will call x for us |
This only leaves us with the first problem: constructing an arbitrary |
string. It turns out that this is the hard part. |
----[ 5.2 First steps in constructing a string |
The method String.fromCharCode immediately comes to mind. It returns a |
string consisting of one or more UTF-16 code units passed as arguments: |
> String.fromCharCode(0x41, 0x42, 0x43) |
'ABC' |
This would be perfect for us, as integers are no problem for WASM. Though, |
the problem is of course that fromCharCode is part of the String global, |
not Object. Luckily, there is a way to obtain it. We'll need to perform the |
following operations, sketched out in JavaScript for readability: |
// Obtain any string |
str = "foobar"; |
// Get the String constructor (i.e. the String global) |
string_constructor = str.constructor; |
// Get String.fromCharCode |
fromCharCode = string_constructor.fromCharCode; |
Obtaining an initial string is a bit tricky, but doable. For example, to |
get the literal string "length", we can do the following: |
empty_object = importObject.constructor.prototype; // {}, importable |
empty_array = Object.values(empty_object); // [] |
Object.getOwnPropertyNames(empty_array); // [ 'length' ] |
The result is still wrapped in an array, but we can use Object.groupBy to |
help with that. A key insight here is that we can craft arbitrary callback |
functions because it is perfectly legal to pass a WASM function using |
ref.func instead of a JavaScript function reference (an externref). So, we |
could craft a "save-the-nth-element" function by looking at the second |
argument passed to the callback, the index. In pseudo-JavaScript: |
g_n = null; |
save_first_elem = (x, i) => (if(i == 0) {g_n = x}); |
arr = [ 'length' ]; |
Object.groupBy(arr, save_first_elem); |
// g_n == 'length' |
Generalized, we can define $array_get_nth_element as follows in WASM: |
;; Callback to use with Object.groupBy() to extract element $n to $g_n. |
(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) |
) |
) |
) |
;; Given $arr and $n, return $arr[n] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.