Next iteration on creating a documentation for the Javascript module.

This commit is contained in:
Oliver Buchtala 2014-02-13 19:25:16 +01:00
commit 1e34ecdc31
4 changed files with 203 additions and 2325 deletions

View file

@ -1,220 +0,0 @@
# Mapping C++ language features to Javascript
## Namespaces
A namespace is represented as a static instance (global variable)
containing other namespaces, global variables and functions
and class templates
### Example:
C++:
~~~~c++
namespace foo {
int x;
namespace bar {
double y;
}
}
~~~~
Javascript:
~~~~javascript
var foo = new Object();
foo.x = 0;
foo.bar = new Object();
foo.bar.y = 0.0;
~~~~
## Global variables and functions
Global variables and functions are properties of other context objects
(global or namespaces).
### Example:
C++:
~~~~c++
int x;
namespace foo {
void bar();
}
~~~~
Javascript:
~~~~javascript
var x = 0;
var foo = new Object();
foo.bar = function() {
return undefined;
}
~~~~
## Classes
Classes are defined as class templates and instantiated using the `new`
operator.
Class members are set in the constructor function using the `this` reference.
Private class members are set using the `var` keyword.
### Example:
C++:
~~~~c++
class Foo {
int bar();
private:
int x;
}
~~~~
Javascript:
~~~~javascript
var Foo = function() {
var x = 42;
this.bar = function() { return x; };
};
var foo = new Foo();
foo.bar();
~~~~
## Static class members and functions
Static variables and functions should be added to the class template object.
~~~~c++
class Foo {
static std::string foo();
std::string bar();
}
~~~~
Javascript:
~~~~javascript
var Foo = function() {
this.bar = function() {
return "bar";
}
};
Foo.foo = function() {
return "foo";
};
var foo = new Foo();
foo.foo()
> TypeError: Object [object Object] has no method 'foo'
Foo.foo()
> "foo"
foo.bar();
> "bar"
~~~~
## Inheritance
Javascript uses a prototype based inheritance mechanism. This limits
feature support to single inheritance.
### Example:
C++:
~~~~c++
class Foo {
public:
int foo();
private:
int x;
}
class Bar: public Foo {
public:
int bar();
}
~~~~
Javascript:
~~~~javascript
var Foo = function() {
var x = 42;
this.foo = function() { return x; };
};
var Bar = function() {
this.bar = function() { return 6; };
}
Bar.prototype = new Foo();
Bar.prototype.constructor = Bar;
var foo = new Foo();
var bar = new Bar();
foo.foo()
> 42
foo.bar()
> TypeError: Object [object Object] has no method 'bar'
bar.foo()
> 42
bar.bar()
> 6
~~~~
## Virtual methods
The prototype mechanism of Javascript allows method overriding which is
needed to map the concept of virtual functions.
### Example:
C++:
~~~~c++
class Foo {
public:
virtual int foo();
}
class Bar: public Foo {
public:
virtual int foo();
}
~~~~
Javascript:
~~~~javascript
var Foo = function() {
this.foo = function() { return 1; };
};
var Bar = function() {}
Bar.prototype = new Foo();
Bar.prototype.constructor = Bar;
Bar.prototype.foo = function() { return 42; };
var foo = new Foo();
var bar = new Bar();
foo.foo()
> 1
bar.foo()
> 42
~~~~
## Overloading
In Javascript like in other scripting languages method overloading is not
available. I.e., there can only be one function for one function name.
Therefore, it is necessary to implement a method dispatching mechanism
for these methods.

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -23,6 +23,17 @@ bool js_template_enable_debug = false;
#define DTOR "dtor"
#define ARGCOUNT "wrap:argc"
// keys for global state variables
#define CREATE_NAMESPACES "create_namespaces"
#define REGISTER_NAMESPACES "register_namespaces"
#define INITIALIZER "initializer"
// keys for class scoped state variables
#define MEMBER_VARIABLES "member_variables"
#define MEMBER_FUNCTIONS "member_functions"
#define STATIC_FUNCTIONS "static_functions"
#define STATIC_VARIABLES "static_variables"
/**
* A convenience class to manage state variables for emitters.
* The implementation delegates to swig Hash DOHs and provides
@ -274,15 +285,15 @@ protected:
File *f_wrappers;
};
/**********************************************************************
* JAVASCRIPT: swig module implementation
**********************************************************************/
/* factory methods for concrete JSEmitters: */
JSEmitter *swig_javascript_create_JSCEmitter();
JSEmitter *swig_javascript_create_V8Emitter();
/**********************************************************************
* JAVASCRIPT: swig module implementation
**********************************************************************/
class JAVASCRIPT:public Language {
public:
@ -1411,17 +1422,6 @@ private:
};
// keys for global state variables
#define CREATE_NAMESPACES "create_namespaces"
#define REGISTER_NAMESPACES "register_namespaces"
#define INITIALIZER "initializer"
// keys for class scoped state variables
#define MEMBER_VARIABLES "member_variables"
#define MEMBER_FUNCTIONS "member_functions"
#define STATIC_FUNCTIONS "static_functions"
#define STATIC_VARIABLES "static_variables"
JSCEmitter::JSCEmitter()
: JSEmitter(),
NULL_STR(NewString("NULL")),