Created
October 19, 2023 21:09
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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