First let’s discuss about some of the component we will use
JSContextRef is the equivalent to JSContext in Objective-C, this where the global object reside and other execution state get saved.
Now you have a basic understanding of the framework let’s start coding
When the above code is executed, it should print the following result:
Lets discus what we did
The contextGroup object is the virtual machine and here everything happen.
The globalContext object is the execution envirement, the first parameter is the virtual machine object and the second parameter is the class of the root object we pass nullptr to use the default class.
Extending the native function
We will extend the callback function so it will log the parameters pased to it, the callback function should look like this:
The above code represent a small implementation of the filesystem, lets start by describing the classDefinition.
The staticFunctions array represent the properties of the Filesystem JavaSciprt class it must be terminated with a null JSStaticValue. We set the kJSPropertyAttributeReadOnly attribute to indicate that this is a read only property, kJSPropertyAttributeDontDelete mean it should not be deleted.
The staticValues array represent the values of the class, the first item is the name the second item is the getter method and the third item is the setter and the fourth item is the attributes, when we want to create a read only property we simply don't provide a setter method.
The callAsConstructor property is our constructor Filesystem_CallAsConstructor, for simplicity our constructor does not check for the validity of the arguments passed to it and assumes everything is OK, the second parameter represent the object constructed by the base class constructor we allocate a new FilesystemPrivate struct and assign it to our object by calling JSObjectSetPrivate.
The setAttributes function takes a pointer to a FilesystemPrivate struct and a path and do some validity checks.
Finnaly we define our class to JavaScipt by calling JSObjectSetProperty.
When i executed the abouve code on my computer i got the following result, "there was a file named file on my desktop"