Skip to content

Instantly share code, notes, and snippets.

@tmikov
Created October 19, 2023 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmikov/75b2a9e8826ee6f9fbb527b5482516d7 to your computer and use it in GitHub Desktop.
Save tmikov/75b2a9e8826ee6f9fbb527b5482516d7 to your computer and use it in GitHub Desktop.
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.
$natives.initMyClass(this);
}
// Attach the native methods like regular JS methods.
// Each native method when invoked will call jsi::Object::getNativeState()
// (checking whether it exists and is the correct C++ class) and then do
// whatever it needs to do.
MyClass.prototype.method1 = $natives.method1;
MyClass.prototype.method2 = $natives.method2;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment