Skip to content

Instantly share code, notes, and snippets.

@tmikov
tmikov / demo.js
Created December 24, 2024 01:44
Show some JS features when compiled AOT to Wasm
function createCounterWithGenerator() {
let count = 0; // Shared mutable variable
const increment = (by = 1) => count += by;
function* doSteps(steps) {
for (let i = 0; i < steps; i++)
yield increment(); // Use the default parameter for `by` in `increment`
}
@tmikov
tmikov / nativestate.js
Last active August 8, 2024 10:43
Example usage of NativeState
/// Pseudocode implementation of jsi::Object::setNativeState()
function setNativeState(obj, ns) { obj.__ns = ns; }
/// Pseudocode implementation of jsi::object::getNativeState()
function getNativeState(obj) { return obj.__ns; }
/// This is a HostFunction, shown as JS pseudo-code
/// Create and initialize NativeState instance.
function _installNativeState(constructor, object, ...args) {
switch (constructor.name) {
case "Camera":
@tmikov
tmikov / small.js
Created April 5, 2024 01:15
A small JS file
print("Hello");
function small(a, f) {
globalThis.inner = [];
globalThis.inner.push( function () {
a[f(a)] = a[f(a+0) + 5];
});
}
@tmikov
tmikov / large.js
Created April 5, 2024 01:15
A large file that optionally touches a fraction of the code
This file has been truncated, but you can view the full file.
print("Hello");
var run = true;
var ratio = 0.5;
if (run) {
large([0,1,2,3,4,5], function f(){ return 0; });
for(let i = 0; i < globalThis.inner.length * ratio; ++i)
globalThis.inner[i]();
}
@tmikov
tmikov / native-state-example.js
Created October 19, 2023 21:09
Example of using NativeState to create a class with native data and methods.
// Call into a native function that returns an object with all native
// functions.
let $natives = getNativeFuncs();
function MyClass(a, b) {
this.a = a;
this.b = b;
// The following call performs the native initialization. It creates a
// C++ NativeState instance, initializes it with whatever it needs, and
// calls jsi::Object::setNativeState() on the object to attach it.