Skip to content

JSLanguageFeaturesJSVvsV8

oliver---- edited this page Apr 22, 2012 · 2 revisions

Transfering C++ language features to Javascript

Here we gather ideas and knowledge how to transfer C++ to JS in general and how to implement it for v8 and JSC, respectively. The aim is to find a unified solution.

Classes

Class template

v8: a persistent V8::FunctionTemplate instance is created once providing a ctor wrapper function. The ctor function is registered as a variable in the surrounding context with the class's name.

JSC: a ctor function is created given a (static) JSClassRef and the ctor function wrapper. The ctor function is registered in the surrounding context under the class's name.

Static variables and functions

v8: Static members are created by manipulating the so called InstanceTemplate with API functions. I.e., a module initialize function needs to be created during processing of variables and functions.

JSC: during the creation of the JSClassRef definition.static_values and definition.static_functions can be set which are tables defining properties and functions, respectively. I.e., tables have to be created during the processing of variables and functions.

Class member variables and functions

v8: same way as with static members but manipulating the PrototypeTemplate.

JSC: Do not know. There is JSObjectSetPrototype, though, it is called on an instance and not on the JSClassRef.

Inheritance

Obviously, multiple inheritance can not transferred to JS.

v8: the chain is created by calling Inherit on the class template.

JSC: do not know. For the creation of JSClassRef definition.parent can be specified.

Functions

Overloading

Similar to other scripting language overloaded functions must be implemented using a dispatcher function that checks the number of arguments and maybe the type. Have a look at the Python module.

Return value and GC

v8: every method wrapper opens a Scope. One argument can be returned (without persisting) and ownership is transferred automatically. All other handles are freed automatically.

  v8::HandleScope scope;
  ...
  return scope.Close(jsresult);

JSC: do not know.

Namespaces

~ Classes. This sounds easy at first, though, for that a context stack (or something equivalent) is necessary. BTW, nested classes do not need to be considered as it isn't support by SWIG currently.

Structs

~ Classes.

Enums

Look at Python module.

Constants

Read-only static variables.

Callbacks/ Function Ptrs

Do not know yet.

Director

Do not know yet.