Beautified Javascript module.

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

View file

@ -303,9 +303,8 @@ class JAVASCRIPT:public Language {
public: public:
JAVASCRIPT(): emitter(NULL) {} JAVASCRIPT():emitter(NULL) {
} ~JAVASCRIPT() {
~JAVASCRIPT() {
delete emitter; delete 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)
{
} }
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
@ -810,7 +804,6 @@ int JSEmitter::enterVariable(Node *n) {
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);
@ -933,7 +926,6 @@ 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.
@ -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))
@ -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)
@ -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");
@ -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() {
@ -1491,7 +1476,6 @@ void JSCEmitter::marshalInputArgs(Node *n, ParmList *parms, Wrapper *wrapper, Ma
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("");
@ -1860,20 +1844,15 @@ private:
}; };
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");
@ -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,8 +1961,7 @@ 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))
@ -2031,7 +2006,6 @@ 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))
@ -2043,8 +2017,7 @@ int V8Emitter::exitClass(Node *n)
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,8 +2026,7 @@ 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");
@ -2062,8 +2034,7 @@ int V8Emitter::exitVariable(Node* n)
.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,7 +2073,6 @@ 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)) {
@ -2143,7 +2112,6 @@ void V8Emitter::marshalInputArgs(Node *n, ParmList *parms, Wrapper *wrapper, Mar
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);
@ -2280,21 +2244,18 @@ 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);
@ -2302,13 +2263,11 @@ DOH *JSEmitterState::clazz(const char* key, DOH *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);
@ -2316,13 +2275,11 @@ DOH *JSEmitterState::function(const char* key, DOH *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);
@ -2331,8 +2288,7 @@ DOH *JSEmitterState::variable(const char* key, DOH *initial)
} }
/*static*/ /*static*/
int JSEmitterState::IsSet(DOH *val) int JSEmitterState::IsSet(DOH *val) {
{
if (!val) { if (!val) {
return 0; return 0;
} else { } else {
@ -2402,10 +2358,12 @@ String *Template::str() {
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) {