Beautified Javascript module.

This commit is contained in:
Oliver Buchtala 2014-03-05 01:39:26 +01:00
commit 89610e984e

View file

@ -50,27 +50,27 @@ public:
~JSEmitterState(); ~JSEmitterState();
DOH *global(); DOH *global ();
DOH *global(const char* key, DOH *initial = 0); DOH *global (const char *key, DOH *initial = 0);
DOH *clazz(bool reset = false); DOH *clazz(bool reset = false);
DOH *clazz(const char* key, DOH *initial = 0); DOH *clazz(const char *key, DOH *initial = 0);
DOH *function(bool reset = false); DOH *function(bool reset = false);
DOH *function(const char* key, DOH *initial = 0); DOH *function(const char *key, DOH *initial = 0);
DOH *variable(bool reset = false); DOH *variable(bool reset = false);
DOH *variable(const char* key, DOH *initial = 0); DOH *variable(const char *key, DOH *initial = 0);
static int IsSet(DOH *val); static int IsSet(DOH *val);
private: private:
DOH *getState(const char* key, bool reset = false); DOH *getState(const char *key, bool reset = false);
Hash *_global; Hash *_global;
}; };
@ -86,21 +86,21 @@ public:
Template(const String *code, const String *templateName); Template(const String *code, const String *templateName);
Template(const Template& other); Template(const Template & other);
~Template(); ~Template();
String *str(); String *str();
Template& replace(const String *pattern, const String *repl); Template & replace(const String *pattern, const String *repl);
Template& print(DOH *doh); Template & print(DOH *doh);
Template& pretty_print(DOH *doh); Template & pretty_print(DOH *doh);
void operator=(const Template& t); void operator=(const Template & t);
Template& trim(); Template & trim();
private: private:
@ -135,7 +135,7 @@ public:
JSEmitter(); JSEmitter();
virtual ~JSEmitter(); virtual ~ JSEmitter();
/** /**
* Opens output files and temporary output DOHs. * Opens output files and temporary output DOHs.
@ -220,7 +220,7 @@ public:
*/ */
Template getTemplate(const String *name); Template getTemplate(const String *name);
State &getState(); State & getState();
protected: protected:
@ -255,7 +255,7 @@ protected:
virtual String *emitInputTypemap(Node *n, Parm *params, Wrapper *wrapper, String *arg); virtual String *emitInputTypemap(Node *n, Parm *params, Wrapper *wrapper, String *arg);
virtual void marshalOutput(Node *n, ParmList *params, Wrapper *wrapper, String *actioncode, const String *cresult=0, bool emitReturnVariable = true); virtual void marshalOutput(Node *n, ParmList *params, Wrapper *wrapper, String *actioncode, const String *cresult = 0, bool emitReturnVariable = true);
virtual void emitCleanupCode(Node *n, Wrapper *wrapper, ParmList *params); virtual void emitCleanupCode(Node *n, Wrapper *wrapper, ParmList *params);
@ -303,9 +303,8 @@ class JAVASCRIPT:public Language {
public: public:
JAVASCRIPT(): emitter(NULL) {} JAVASCRIPT():emitter(NULL) {
} ~JAVASCRIPT() {
~JAVASCRIPT() {
delete emitter; delete emitter;
} }
@ -327,7 +326,7 @@ public:
private: private:
JSEmitter *emitter; JSEmitter * emitter;
}; };
/* --------------------------------------------------------------------- /* ---------------------------------------------------------------------
@ -591,12 +590,7 @@ extern "C" Language *swig_javascript(void) {
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
JSEmitter::JSEmitter() JSEmitter::JSEmitter()
: templates(NewHash()), : templates(NewHash()), namespaces(NULL), current_namespace(NULL), defaultResultName(NewString("result")), f_wrappers(NULL) {
namespaces(NULL),
current_namespace(NULL),
defaultResultName(NewString("result")),
f_wrappers(NULL)
{
} }
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
@ -614,8 +608,8 @@ JSEmitter::~JSEmitter() {
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
int JSEmitter::registerTemplate(const String *name, const String *code) { int JSEmitter::registerTemplate(const String *name, const String *code) {
if (!State::IsSet(state.global(HAS_TEMPLATES))) { if (!State::IsSet(state.global (HAS_TEMPLATES))) {
SetFlag(state.global(), HAS_TEMPLATES); SetFlag(state.global (), HAS_TEMPLATES);
} }
return Setattr(templates, name, code); return Setattr(templates, name, code);
} }
@ -636,13 +630,13 @@ Template JSEmitter::getTemplate(const String *name) {
return t; return t;
} }
JSEmitterState &JSEmitter::getState() { JSEmitterState & JSEmitter::getState() {
return state; return state;
} }
int JSEmitter::initialize(Node * /*n*/) { int JSEmitter::initialize(Node * /*n */ ) {
if(namespaces != NULL) { if (namespaces != NULL) {
Delete(namespaces); Delete(namespaces);
} }
namespaces = NewHash(); namespaces = NewHash();
@ -784,7 +778,7 @@ int JSEmitter::enterClass(Node *n) {
int JSEmitter::enterFunction(Node *n) { int JSEmitter::enterFunction(Node *n) {
state.function(RESET); state.function(RESET);
state.function(NAME, Getattr(n, "sym:name")); state.function(NAME, Getattr(n, "sym:name"));
if(Equal(Getattr(n, "storage"), "static")) { if (Equal(Getattr(n, "storage"), "static")) {
SetFlag(state.function(), IS_STATIC); SetFlag(state.function(), IS_STATIC);
} }
return SWIG_OK; return SWIG_OK;
@ -803,14 +797,13 @@ int JSEmitter::enterVariable(Node *n) {
state.variable(NAME, Swig_scopename_last(Getattr(n, "sym:name"))); state.variable(NAME, Swig_scopename_last(Getattr(n, "sym:name")));
} }
if(Equal(Getattr(n, "storage"), "static")) { if (Equal(Getattr(n, "storage"), "static")) {
SetFlag(state.variable(), IS_STATIC); SetFlag(state.variable(), IS_STATIC);
} }
if (!Language::instance()->is_assignable(n)) { if (!Language::instance()->is_assignable(n)) {
SetFlag(state.variable(), IS_IMMUTABLE); SetFlag(state.variable(), IS_IMMUTABLE);
} }
// FIXME: test "arrays_global" does not compile with that as it is not allowed to assign to char[] // FIXME: test "arrays_global" does not compile with that as it is not allowed to assign to char[]
if (Equal(Getattr(n, "type"), "a().char")) { if (Equal(Getattr(n, "type"), "a().char")) {
SetFlag(state.variable(), IS_IMMUTABLE); SetFlag(state.variable(), IS_IMMUTABLE);
@ -829,7 +822,7 @@ int JSEmitter::emitCtor(Node *n) {
//String *mangled_name = SwigType_manglestr(Getattr(n, "name")); //String *mangled_name = SwigType_manglestr(Getattr(n, "name"));
String *wrap_name = Swig_name_wrapper(Getattr(n, "sym:name")); String *wrap_name = Swig_name_wrapper(Getattr(n, "sym:name"));
if(is_overloaded) { if (is_overloaded) {
t_ctor = getTemplate("js_overloaded_ctor"); t_ctor = getTemplate("js_overloaded_ctor");
Append(wrap_name, Getattr(n, "sym:overname")); Append(wrap_name, Getattr(n, "sym:overname"));
} }
@ -869,7 +862,7 @@ int JSEmitter::emitCtor(Node *n) {
DelWrapper(wrapper); DelWrapper(wrapper);
// create a dispatching ctor // create a dispatching ctor
if(is_overloaded) { if (is_overloaded) {
if (!Getattr(n, "sym:nextSibling")) { if (!Getattr(n, "sym:nextSibling")) {
String *wrap_name = Swig_name_wrapper(Getattr(n, "sym:name")); String *wrap_name = Swig_name_wrapper(Getattr(n, "sym:name"));
Template t_mainctor(getTemplate("js_ctor_dispatcher")); Template t_mainctor(getTemplate("js_ctor_dispatcher"));
@ -933,17 +926,16 @@ int JSEmitter::emitDtor(Node *n) {
} }
} }
// HACK: this is only for the v8 emitter. maybe set an attribute wrap:action of node // HACK: this is only for the v8 emitter. maybe set an attribute wrap:action of node
// TODO: generate dtors more similar to other wrappers // TODO: generate dtors more similar to other wrappers
// EW: I think this is wrong. delete should only be used when new was used to create. If malloc was used, free needs to be used. // EW: I think this is wrong. delete should only be used when new was used to create. If malloc was used, free needs to be used.
if(SwigType_isarray(type)) { if (SwigType_isarray(type)) {
Printf(free, "delete [] (%s)", ctype); Printf(free, "delete [] (%s)", ctype);
} else { } else {
Printf(free, "delete (%s)", ctype); Printf(free, "delete (%s)", ctype);
} }
String* destructor_action = Getattr(n, "wrap:action"); String *destructor_action = Getattr(n, "wrap:action");
// Adapted from the JSCore implementation. // Adapted from the JSCore implementation.
/* The next challenge is to generate the correct finalize function for JavaScriptCore to call. /* The next challenge is to generate the correct finalize function for JavaScriptCore to call.
Originally, it would use this fragment from javascriptcode.swg Originally, it would use this fragment from javascriptcode.swg
@ -985,7 +977,7 @@ int JSEmitter::emitDtor(Node *n) {
Maybe the fix for the destructor_action always true problem is that this is supposed to be embedded in the if(Extend) block above. Maybe the fix for the destructor_action always true problem is that this is supposed to be embedded in the if(Extend) block above.
But I don't fully understand the conditions of any of these things, and since it works for the moment, I don't want to break more stuff. But I don't fully understand the conditions of any of these things, and since it works for the moment, I don't want to break more stuff.
*/ */
if(destructor_action) { if (destructor_action) {
Template t_dtor = getTemplate("js_dtoroverride"); Template t_dtor = getTemplate("js_dtoroverride");
state.clazz(DTOR, wrap_name); state.clazz(DTOR, wrap_name);
t_dtor.replace("${classname_mangled}", state.clazz(NAME_MANGLED)) t_dtor.replace("${classname_mangled}", state.clazz(NAME_MANGLED))
@ -995,8 +987,7 @@ int JSEmitter::emitDtor(Node *n) {
t_dtor.replace("${destructor_action}", destructor_action); t_dtor.replace("${destructor_action}", destructor_action);
Wrapper_pretty_print(t_dtor.str(), f_wrappers); Wrapper_pretty_print(t_dtor.str(), f_wrappers);
} } else {
else {
Template t_dtor = getTemplate("js_dtor"); Template t_dtor = getTemplate("js_dtor");
state.clazz(DTOR, wrap_name); state.clazz(DTOR, wrap_name);
t_dtor.replace("$jsmangledname", state.clazz(NAME_MANGLED)) t_dtor.replace("$jsmangledname", state.clazz(NAME_MANGLED))
@ -1091,7 +1082,7 @@ int JSEmitter::emitConstant(Node *n) {
// a lot of SWIG internal constants were emitted // a lot of SWIG internal constants were emitted
// This didn't happen on other platforms yet... // This didn't happen on other platforms yet...
// we ignore those premature definitions // we ignore those premature definitions
if (!State::IsSet(state.global(HAS_TEMPLATES))) { if (!State::IsSet(state.global (HAS_TEMPLATES))) {
return SWIG_ERROR; return SWIG_ERROR;
} }
@ -1197,8 +1188,7 @@ int JSEmitter::emitFunctionDispatcher(Node *n, bool /*is_member */ ) {
do { do {
String *siblname = Getattr(sibl, "wrap:name"); String *siblname = Getattr(sibl, "wrap:name");
if (siblname) if (siblname) {
{
// handle function overloading // handle function overloading
Template t_dispatch_case = getTemplate("js_function_dispatch_case"); Template t_dispatch_case = getTemplate("js_function_dispatch_case");
t_dispatch_case.replace("$jswrapper", siblname) t_dispatch_case.replace("$jswrapper", siblname)
@ -1219,7 +1209,7 @@ int JSEmitter::emitFunctionDispatcher(Node *n, bool /*is_member */ ) {
String *overname = Getattr(n, "sym:overname"); String *overname = Getattr(n, "sym:overname");
int l1 = Len(wrap_name); int l1 = Len(wrap_name);
int l2 = Len(overname); int l2 = Len(overname);
Delslice(wrap_name, l1-l2, l1); Delslice(wrap_name, l1 - l2, l1);
Setattr(n, "wrap:name", wrap_name); Setattr(n, "wrap:name", wrap_name);
state.function(WRAPPER_NAME, wrap_name); state.function(WRAPPER_NAME, wrap_name);
@ -1267,9 +1257,11 @@ void JSEmitter::marshalOutput(Node *n, ParmList *params, Wrapper *wrapper, Strin
Parm *p; Parm *p;
// adds a declaration for the result variable // adds a declaration for the result variable
if(emitReturnVariable) emit_return_variable(n, type, wrapper); if (emitReturnVariable)
emit_return_variable(n, type, wrapper);
// if not given, use default result identifier ('result') for output typemap // if not given, use default result identifier ('result') for output typemap
if(cresult == 0) cresult = defaultResultName; if (cresult == 0)
cresult = defaultResultName;
tm = Swig_typemap_lookup_out("out", n, cresult, wrapper, actioncode); tm = Swig_typemap_lookup_out("out", n, cresult, wrapper, actioncode);
bool should_own = GetFlag(n, "feature:new"); bool should_own = GetFlag(n, "feature:new");
@ -1348,7 +1340,7 @@ int JSEmitter::switchNamespace(Node *n) {
if (nspace == NULL) { if (nspace == NULL) {
// enums and constants do not have 'sym:nspace' set // enums and constants do not have 'sym:nspace' set
// so we try to get the namespace from the qualified name // so we try to get the namespace from the qualified name
if(Equal(Getattr(n, "nodeType"), "enumitem")) { if (Equal(Getattr(n, "nodeType"), "enumitem")) {
nspace = Swig_scopename_prefix(Getattr(n, "name")); nspace = Swig_scopename_prefix(Getattr(n, "name"));
} }
} }
@ -1457,14 +1449,7 @@ private:
}; };
JSCEmitter::JSCEmitter() JSCEmitter::JSCEmitter()
: JSEmitter(), : JSEmitter(), NULL_STR(NewString("NULL")), VETO_SET(NewString("JS_veto_set_variable")), f_wrap_cpp(NULL), f_runtime(NULL), f_header(NULL), f_init(NULL) {
NULL_STR(NewString("NULL")),
VETO_SET(NewString("JS_veto_set_variable")),
f_wrap_cpp(NULL),
f_runtime(NULL),
f_header(NULL),
f_init(NULL)
{
} }
JSCEmitter::~JSCEmitter() { JSCEmitter::~JSCEmitter() {
@ -1488,10 +1473,9 @@ void JSCEmitter::marshalInputArgs(Node *n, ParmList *parms, Wrapper *wrapper, Ma
// determine an offset index, as members have an extra 'this' argument // determine an offset index, as members have an extra 'this' argument
// except: static members and ctors. // except: static members and ctors.
int startIdx = 0; int startIdx = 0;
if (is_member && !is_static && mode!=Ctor) { if (is_member && !is_static && mode != Ctor) {
startIdx = 1; startIdx = 1;
} }
// store number of arguments for argument checks // store number of arguments for argument checks
int num_args = emit_num_arguments(parms) - startIdx; int num_args = emit_num_arguments(parms) - startIdx;
String *argcount = NewString(""); String *argcount = NewString("");
@ -1559,9 +1543,9 @@ int JSCEmitter::initialize(Node *n) {
f_init = NewString(""); f_init = NewString("");
f_header = NewString(""); f_header = NewString("");
state.global(CREATE_NAMESPACES, NewString("")); state.global (CREATE_NAMESPACES, NewString(""));
state.global(REGISTER_NAMESPACES, NewString("")); state.global (REGISTER_NAMESPACES, NewString(""));
state.global(INITIALIZER, NewString("")); state.global (INITIALIZER, NewString(""));
/* Register file targets with the SWIG file handler */ /* Register file targets with the SWIG file handler */
Swig_register_filebyname("begin", f_wrap_cpp); Swig_register_filebyname("begin", f_wrap_cpp);
@ -1594,9 +1578,9 @@ int JSCEmitter::dump(Node *n) {
// compose the initializer function using a template // compose the initializer function using a template
Template initializer(getTemplate("js_initializer")); Template initializer(getTemplate("js_initializer"));
initializer.replace("$jsname", module) initializer.replace("$jsname", module)
.replace("$jsregisterclasses", state.global(INITIALIZER)) .replace("$jsregisterclasses", state.global (INITIALIZER))
.replace("$jscreatenamespaces", state.global(CREATE_NAMESPACES)) .replace("$jscreatenamespaces", state.global (CREATE_NAMESPACES))
.replace("$jsregisternamespaces", state.global(REGISTER_NAMESPACES)) .replace("$jsregisternamespaces", state.global (REGISTER_NAMESPACES))
.pretty_print(f_init); .pretty_print(f_init);
Printv(f_wrap_cpp, f_init, 0); Printv(f_wrap_cpp, f_init, 0);
@ -1711,7 +1695,7 @@ int JSCEmitter::exitClass(Node *n) {
//Append(f_wrappers, state.clazz(CTOR_WRAPPERS)); //Append(f_wrappers, state.clazz(CTOR_WRAPPERS));
// for abstract classes add a vetoing ctor // for abstract classes add a vetoing ctor
if(GetFlag(state.clazz(), IS_ABSTRACT)) { if (GetFlag(state.clazz(), IS_ABSTRACT)) {
Template t_veto_ctor(getTemplate("js_veto_ctor")); Template t_veto_ctor(getTemplate("js_veto_ctor"));
t_veto_ctor.replace("$jswrapper", state.clazz(CTOR)) t_veto_ctor.replace("$jswrapper", state.clazz(CTOR))
.replace("$jsname", state.clazz(NAME)) .replace("$jsname", state.clazz(NAME))
@ -1740,7 +1724,7 @@ int JSCEmitter::exitClass(Node *n) {
.replace("$jsclass_inheritance", jsclass_inheritance) .replace("$jsclass_inheritance", jsclass_inheritance)
.replace("$jsctor", state.clazz(CTOR)) .replace("$jsctor", state.clazz(CTOR))
.replace("$jsdtor", state.clazz(DTOR)) .replace("$jsdtor", state.clazz(DTOR))
.pretty_print(state.global(INITIALIZER)); .pretty_print(state.global (INITIALIZER));
Delete(jsclass_inheritance); Delete(jsclass_inheritance);
/* Note: this makes sure that there is a swig_type added for this class */ /* Note: this makes sure that there is a swig_type added for this class */
@ -1750,8 +1734,8 @@ int JSCEmitter::exitClass(Node *n) {
Template t_registerclass(getTemplate("jsc_class_registration")); Template t_registerclass(getTemplate("jsc_class_registration"));
t_registerclass.replace("$jsname", state.clazz(NAME)) t_registerclass.replace("$jsname", state.clazz(NAME))
.replace("$jsmangledname", state.clazz(NAME_MANGLED)) .replace("$jsmangledname", state.clazz(NAME_MANGLED))
.replace("$jsnspace", Getattr(state.clazz("nspace"),NAME_MANGLED)) .replace("$jsnspace", Getattr(state.clazz("nspace"), NAME_MANGLED))
.pretty_print(state.global(INITIALIZER)); .pretty_print(state.global (INITIALIZER));
return SWIG_OK; return SWIG_OK;
} }
@ -1784,7 +1768,7 @@ int JSCEmitter::emitNamespaces() {
Template t_createNamespace(getTemplate("jsc_nspace_definition")); Template t_createNamespace(getTemplate("jsc_nspace_definition"));
t_createNamespace.replace("$jsmangledname", name_mangled); t_createNamespace.replace("$jsmangledname", name_mangled);
Append(state.global(CREATE_NAMESPACES), t_createNamespace.str()); Append(state.global (CREATE_NAMESPACES), t_createNamespace.str());
// Don't register 'exports' as namespace. It is return to the application. // Don't register 'exports' as namespace. It is return to the application.
if (!Equal("exports", name)) { if (!Equal("exports", name)) {
@ -1792,7 +1776,7 @@ int JSCEmitter::emitNamespaces() {
t_registerNamespace.replace("$jsmangledname", name_mangled) t_registerNamespace.replace("$jsmangledname", name_mangled)
.replace("$jsname", name) .replace("$jsname", name)
.replace("$jsparent", parent_mangled); .replace("$jsparent", parent_mangled);
Append(state.global(REGISTER_NAMESPACES), t_registerNamespace.str()); Append(state.global (REGISTER_NAMESPACES), t_registerNamespace.str());
} }
} }
@ -1808,13 +1792,13 @@ JSEmitter *swig_javascript_create_JSCEmitter() {
* V8: JSEmitter implementation for V8 engine * V8: JSEmitter implementation for V8 engine
**********************************************************************/ **********************************************************************/
class V8Emitter: public JSEmitter { class V8Emitter:public JSEmitter {
public: public:
V8Emitter(); V8Emitter();
virtual ~V8Emitter(); virtual ~ V8Emitter();
virtual int initialize(Node *n); virtual int initialize(Node *n);
virtual int dump(Node *n); virtual int dump(Node *n);
virtual int close(); virtual int close();
@ -1853,33 +1837,28 @@ private:
// the output cpp file // the output cpp file
File *f_wrap_cpp; File *f_wrap_cpp;
String* NULL_STR; String *NULL_STR;
String *VETO_SET; String *VETO_SET;
String *moduleName; String *moduleName;
}; };
V8Emitter::V8Emitter() V8Emitter::V8Emitter()
: JSEmitter(), : JSEmitter(), NULL_STR(NewString("0")), VETO_SET(NewString("JS_veto_set_variable")) {
NULL_STR(NewString("0")),
VETO_SET(NewString("JS_veto_set_variable"))
{
} }
V8Emitter::~V8Emitter() V8Emitter::~V8Emitter() {
{
Delete(NULL_STR); Delete(NULL_STR);
Delete(VETO_SET); Delete(VETO_SET);
} }
int V8Emitter::initialize(Node *n) int V8Emitter::initialize(Node *n) {
{
JSEmitter::initialize(n); JSEmitter::initialize(n);
moduleName = Getattr(n,"name"); moduleName = Getattr(n, "name");
// Get the output file name // Get the output file name
String *outfile = Getattr(n,"outfile"); String *outfile = Getattr(n, "outfile");
f_wrap_cpp = NewFile(outfile, "w", SWIG_output_files()); f_wrap_cpp = NewFile(outfile, "w", SWIG_output_files());
if (!f_wrap_cpp) { if (!f_wrap_cpp) {
FileErrorDisplay(outfile); FileErrorDisplay(outfile);
@ -1912,8 +1891,7 @@ int V8Emitter::initialize(Node *n)
return SWIG_OK; return SWIG_OK;
} }
int V8Emitter::dump(Node *n) int V8Emitter::dump(Node *n) {
{
/* Get the module name */ /* Get the module name */
String *module = Getattr(n, "name"); String *module = Getattr(n, "name");
@ -1953,8 +1931,7 @@ int V8Emitter::dump(Node *n)
return SWIG_OK; return SWIG_OK;
} }
int V8Emitter::close() int V8Emitter::close() {
{
Delete(f_runtime); Delete(f_runtime);
Delete(f_header); Delete(f_header);
Delete(f_class_templates); Delete(f_class_templates);
@ -1972,8 +1949,7 @@ int V8Emitter::close()
return SWIG_OK; return SWIG_OK;
} }
int V8Emitter::enterClass(Node *n) int V8Emitter::enterClass(Node *n) {
{
JSEmitter::enterClass(n); JSEmitter::enterClass(n);
// emit declaration of a v8 class template // emit declaration of a v8 class template
@ -1985,9 +1961,8 @@ int V8Emitter::enterClass(Node *n)
return SWIG_OK; return SWIG_OK;
} }
int V8Emitter::exitClass(Node *n) int V8Emitter::exitClass(Node *n) {
{ if (GetFlag(state.clazz(), IS_ABSTRACT)) {
if(GetFlag(state.clazz(), IS_ABSTRACT)) {
Template t_veto_ctor(getTemplate("js_veto_ctor")); Template t_veto_ctor(getTemplate("js_veto_ctor"));
t_veto_ctor.replace("$jswrapper", state.clazz(CTOR)) t_veto_ctor.replace("$jswrapper", state.clazz(CTOR))
.replace("$jsname", state.clazz(NAME)) .replace("$jsname", state.clazz(NAME))
@ -2018,8 +1993,8 @@ int V8Emitter::exitClass(Node *n)
.pretty_print(f_init_class_instances); .pretty_print(f_init_class_instances);
// emit inheritance setup // emit inheritance setup
Node* baseClass = getBaseClass(n); Node *baseClass = getBaseClass(n);
if(baseClass) { if (baseClass) {
String *base_name = Getattr(baseClass, "name"); String *base_name = Getattr(baseClass, "name");
Template t_inherit = getTemplate("jsv8_inherit"); Template t_inherit = getTemplate("jsv8_inherit");
@ -2031,20 +2006,18 @@ int V8Emitter::exitClass(Node *n)
.pretty_print(f_init_inheritance); .pretty_print(f_init_inheritance);
Delete(base_name_mangled); Delete(base_name_mangled);
} }
// emit registeration of class template // emit registeration of class template
Template t_register = getTemplate("jsv8_register_class"); Template t_register = getTemplate("jsv8_register_class");
t_register.replace("$jsmangledname", state.clazz(NAME_MANGLED)) t_register.replace("$jsmangledname", state.clazz(NAME_MANGLED))
.replace("$jsname", state.clazz(NAME)) .replace("$jsname", state.clazz(NAME))
.replace("$jsparent", Getattr(state.clazz("nspace"),NAME_MANGLED)) .replace("$jsparent", Getattr(state.clazz("nspace"), NAME_MANGLED))
.trim() .trim()
.pretty_print(f_init_register_classes); .pretty_print(f_init_register_classes);
return SWIG_OK; return SWIG_OK;
} }
int V8Emitter::enterVariable(Node* n) int V8Emitter::enterVariable(Node *n) {
{
JSEmitter::enterVariable(n); JSEmitter::enterVariable(n);
state.variable(GETTER, NULL_STR); state.variable(GETTER, NULL_STR);
@ -2053,17 +2026,15 @@ int V8Emitter::enterVariable(Node* n)
return SWIG_OK; return SWIG_OK;
} }
int V8Emitter::exitVariable(Node* n) int V8Emitter::exitVariable(Node *n) {
{ if (GetFlag(n, "ismember")) {
if(GetFlag(n, "ismember")) { if (GetFlag(state.variable(), IS_STATIC) || Equal(Getattr(n, "nodeType"), "enumitem")) {
if(GetFlag(state.variable(), IS_STATIC) || Equal(Getattr(n, "nodeType"), "enumitem") ) {
Template t_register = getTemplate("jsv8_register_static_variable"); Template t_register = getTemplate("jsv8_register_static_variable");
t_register.replace("$jsparent", state.clazz(NAME_MANGLED)) t_register.replace("$jsparent", state.clazz(NAME_MANGLED))
.replace("$jsname", state.variable(NAME)) .replace("$jsname", state.variable(NAME))
.replace("$jsgetter", state.variable(GETTER)) .replace("$jsgetter", state.variable(GETTER))
.replace("$jssetter", state.variable(SETTER)) .replace("$jssetter", state.variable(SETTER))
.trim(). .trim().print(f_init_static_wrappers);
print(f_init_static_wrappers);
} else { } else {
Template t_register = getTemplate("jsv8_register_member_variable"); Template t_register = getTemplate("jsv8_register_member_variable");
t_register.replace("$jsmangledname", state.clazz(NAME_MANGLED)) t_register.replace("$jsmangledname", state.clazz(NAME_MANGLED))
@ -2088,8 +2059,7 @@ int V8Emitter::exitVariable(Node* n)
return SWIG_OK; return SWIG_OK;
} }
int V8Emitter::exitFunction(Node* n) int V8Emitter::exitFunction(Node *n) {
{
bool is_member = GetFlag(n, "ismember") | GetFlag(n, "feature:extend"); bool is_member = GetFlag(n, "ismember") | GetFlag(n, "feature:extend");
// create a dispatcher for overloaded functions // create a dispatcher for overloaded functions
@ -2103,10 +2073,9 @@ int V8Emitter::exitFunction(Node* n)
return SWIG_OK; return SWIG_OK;
} }
} }
// register the function at the specific context // register the function at the specific context
if(is_member) { if (is_member) {
if(GetFlag(state.function(), IS_STATIC)) { if (GetFlag(state.function(), IS_STATIC)) {
Template t_register = getTemplate("jsv8_register_static_function"); Template t_register = getTemplate("jsv8_register_static_function");
t_register.replace("$jsparent", state.clazz(NAME_MANGLED)) t_register.replace("$jsparent", state.clazz(NAME_MANGLED))
.replace("$jsname", state.function(NAME)) .replace("$jsname", state.function(NAME))
@ -2140,10 +2109,9 @@ void V8Emitter::marshalInputArgs(Node *n, ParmList *parms, Wrapper *wrapper, Mar
String *tm; String *tm;
int startIdx = 0; int startIdx = 0;
if (is_member && !is_static && mode!=Ctor) { if (is_member && !is_static && mode != Ctor) {
startIdx = 1; startIdx = 1;
} }
// store number of arguments for argument checks // store number of arguments for argument checks
int num_args = emit_num_arguments(parms) - startIdx; int num_args = emit_num_arguments(parms) - startIdx;
String *argcount = NewString(""); String *argcount = NewString("");
@ -2214,7 +2182,6 @@ int V8Emitter::emitNamespaces() {
if (Equal(parent, "")) { if (Equal(parent, "")) {
do_register = false; do_register = false;
} }
// Note: 'exports' is by convention the name of the object where // Note: 'exports' is by convention the name of the object where
// globals are stored into // globals are stored into
if (Equal(name, "exports")) { if (Equal(name, "exports")) {
@ -2254,21 +2221,18 @@ JSEmitter *swig_javascript_create_V8Emitter() {
**********************************************************************/ **********************************************************************/
JSEmitterState::JSEmitterState() JSEmitterState::JSEmitterState()
: _global(NewHash()) : _global(NewHash()) {
{
// initialize sub-hashes // initialize sub-hashes
Setattr(_global, "class", NewHash()); Setattr(_global, "class", NewHash());
Setattr(_global, "function", NewHash()); Setattr(_global, "function", NewHash());
Setattr(_global, "variable", NewHash()); Setattr(_global, "variable", NewHash());
} }
JSEmitterState::~JSEmitterState() JSEmitterState::~JSEmitterState() {
{
Delete(_global); Delete(_global);
} }
DOH *JSEmitterState::getState(const char* key, bool _new) DOH *JSEmitterState::getState(const char *key, bool _new) {
{
if (_new) { if (_new) {
Hash *hash = NewHash(); Hash *hash = NewHash();
Setattr(_global, key, hash); Setattr(_global, key, hash);
@ -2276,63 +2240,55 @@ DOH *JSEmitterState::getState(const char* key, bool _new)
return Getattr(_global, key); return Getattr(_global, key);
} }
DOH *JSEmitterState::global() { DOH *JSEmitterState::global () {
return _global; return _global;
} }
DOH *JSEmitterState::global(const char* key, DOH *initial) DOH *JSEmitterState::global (const char *key, DOH *initial) {
{ if (initial != 0) {
if(initial != 0) {
Setattr(_global, key, initial); Setattr(_global, key, initial);
} }
return Getattr(_global, key); return Getattr(_global, key);
} }
DOH *JSEmitterState::clazz(bool _new) DOH *JSEmitterState::clazz(bool _new) {
{
return getState("class", _new); return getState("class", _new);
} }
DOH *JSEmitterState::clazz(const char* key, DOH *initial) DOH *JSEmitterState::clazz(const char *key, DOH *initial) {
{
DOH *c = clazz(); DOH *c = clazz();
if(initial != 0) { if (initial != 0) {
Setattr(c, key, initial); Setattr(c, key, initial);
} }
return Getattr(c, key); return Getattr(c, key);
} }
DOH *JSEmitterState::function(bool _new) DOH *JSEmitterState::function(bool _new) {
{
return getState("function", _new); return getState("function", _new);
} }
DOH *JSEmitterState::function(const char* key, DOH *initial) DOH *JSEmitterState::function(const char *key, DOH *initial) {
{
DOH *f = function(); DOH *f = function();
if(initial != 0) { if (initial != 0) {
Setattr(f, key, initial); Setattr(f, key, initial);
} }
return Getattr(f, key); return Getattr(f, key);
} }
DOH *JSEmitterState::variable(bool _new) DOH *JSEmitterState::variable(bool _new) {
{
return getState("variable", _new); return getState("variable", _new);
} }
DOH *JSEmitterState::variable(const char* key, DOH *initial) DOH *JSEmitterState::variable(const char *key, DOH *initial) {
{
DOH *v = variable(); DOH *v = variable();
if(initial != 0) { if (initial != 0) {
Setattr(v, key, initial); Setattr(v, key, initial);
} }
return Getattr(v, key); return Getattr(v, key);
} }
/*static*/ /*static*/
int JSEmitterState::IsSet(DOH *val) int JSEmitterState::IsSet(DOH *val) {
{
if (!val) { if (!val) {
return 0; return 0;
} else { } else {
@ -2400,34 +2356,36 @@ String *Template::str() {
return code; return code;
} }
Template& Template::trim() { Template & Template::trim() {
const char* str = Char(code); const char *str = Char(code);
if (str == 0) return *this; if (str == 0)
return *this;
int length = Len(code); int length = Len(code);
if (length == 0) return *this; if (length == 0)
return *this;
int idx; int idx;
for(idx=0; idx < length; ++idx) { for (idx = 0; idx < length; ++idx) {
if (str[idx] != ' ' && str[idx] != '\t' && str[idx] != '\r' && str[idx] != '\n') if (str[idx] != ' ' && str[idx] != '\t' && str[idx] != '\r' && str[idx] != '\n')
break; break;
} }
int start_pos = idx; int start_pos = idx;
for(idx=length-1; idx >= start_pos; --idx) { for (idx = length - 1; idx >= start_pos; --idx) {
if (str[idx] != ' ' && str[idx] != '\t' && str[idx] != '\r' && str[idx] != '\n') if (str[idx] != ' ' && str[idx] != '\t' && str[idx] != '\r' && str[idx] != '\n')
break; break;
} }
int end_pos = idx; int end_pos = idx;
int new_length = end_pos-start_pos+1; int new_length = end_pos - start_pos + 1;
char* newstr = new char[new_length+1]; char *newstr = new char[new_length + 1];
memcpy(newstr, str+start_pos, new_length); memcpy(newstr, str + start_pos, new_length);
newstr[new_length] = 0; newstr[new_length] = 0;
Delete(code); Delete(code);
code = NewString(newstr); code = NewString(newstr);
delete[] newstr; delete[]newstr;
return *this; return *this;
} }
@ -2442,27 +2400,27 @@ Template& Template::trim() {
* - returns a reference to the Template to allow chaining of methods. * - returns a reference to the Template to allow chaining of methods.
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
Template& Template::replace(const String *pattern, const String *repl) { Template & Template::replace(const String *pattern, const String *repl) {
Replaceall(code, pattern, repl); Replaceall(code, pattern, repl);
return *this; return *this;
} }
Template& Template::print(DOH *doh) { Template & Template::print(DOH *doh) {
Printv(doh, str(), 0); Printv(doh, str(), 0);
return *this; return *this;
} }
Template& Template::pretty_print(DOH *doh) { Template & Template::pretty_print(DOH *doh) {
Wrapper_pretty_print(str(), doh); Wrapper_pretty_print(str(), doh);
return *this; return *this;
} }
Template::Template(const Template& t) { Template::Template(const Template & t) {
code = NewString(t.code); code = NewString(t.code);
templateName = NewString(t.templateName); templateName = NewString(t.templateName);
} }
void Template::operator=(const Template& t) { void Template::operator=(const Template & t) {
Delete(code); Delete(code);
Delete(templateName); Delete(templateName);
code = NewString(t.code); code = NewString(t.code);