// ---------------------------------------------------------------------------- // Description : Javascript interpreter // ---------------------------------------------------------------------------- // (c) Copyright 2000 by iXiONmedia, all rights reserved. // ---------------------------------------------------------------------------- #include #include #define EXJS_ADD_CODE_LOCATION \ catch (no_location_javascript_exception &half) { \ throw javascript_exception(half,getCodeLocation()); \ } using namespace ixion; using namespace javascript; // variable_declaration ------------------------------------------------------- variable_declaration::variable_declaration(string const &id,ref def_value,code_location const &loc) : expression(loc),Identifier(id),DefaultValue(def_value) { } ref variable_declaration::evaluate(context const &ctx) const { try { ref def; if (DefaultValue.get() != NULL) def = DefaultValue->evaluate(ctx)->eliminateWrappers()->duplicate(); else def = makeNull(); ref lv = makeLValue(def); ctx.DeclarationScope->addMember(Identifier,lv); return lv; } EXJS_ADD_CODE_LOCATION } // constant_declaration ------------------------------------------------------- constant_declaration::constant_declaration(string const &id,ref def_value,code_location const &loc) : expression(loc),Identifier(id),DefaultValue(def_value) { } ref constant_declaration::evaluate(context const &ctx) const { try { ref def; if (DefaultValue.get() != NULL) def = DefaultValue->evaluate(ctx)->eliminateWrappers()->duplicate(); else def = makeNull(); ref cns = wrapConstant(def); ctx.DeclarationScope->addMember(Identifier,cns); return cns; } EXJS_ADD_CODE_LOCATION } // function_declaration ------------------------------------------------------- function_declaration:: function_declaration(string const &id,parameter_name_list const &pnames, ref body,code_location const &loc) : expression(loc),Identifier(id),ParameterNameList(pnames),Body(body) { } ref function_declaration::evaluate(context const &ctx) const { try { ref fun = new function(ParameterNameList,Body,ctx.LookupScope); ctx.DeclarationScope->addMember(Identifier,fun); return ref(NULL); } EXJS_ADD_CODE_LOCATION } // method_declaration --------------------------------------------------------- method_declaration:: method_declaration(string const &id,parameter_name_list const &pnames, ref body,code_location const &loc) : expression(loc),Identifier(id),ParameterNameList(pnames),Body(body) { } ref method_declaration::evaluate(context const &ctx) const { try { ref fun = new method(ParameterNameList,Body,ctx.LookupScope); ctx.DeclarationScope->addMember(Identifier,fun); return ref(NULL); } EXJS_ADD_CODE_LOCATION } // constructor_declaration --------------------------------------------------------- constructor_declaration:: constructor_declaration(parameter_name_list const &pnames, ref body,code_location const &loc) : expression(loc),ParameterNameList(pnames),Body(body) { } ref constructor_declaration::evaluate(context const &ctx) const { try { ref fun = new constructor(ParameterNameList,Body,ctx.LookupScope); return fun; } EXJS_ADD_CODE_LOCATION } // js_class_declaration ------------------------------------------------------- js_class_declaration::js_class_declaration(string const &id,ref superclass,code_location const &loc) : expression(loc),Identifier(id),SuperClass(superclass) { } ref js_class_declaration::evaluate(context const &ctx) const { try { ref sml(new list_scope); ref ml(new list_scope); ref svl(new list_scope); ref sc; if (SuperClass.get()) sc = SuperClass->evaluate(ctx); ref constructor; if (ConstructorDeclaration.get()) constructor = ConstructorDeclaration->evaluate(ctx); ref cls(new js_class(ctx.LookupScope,sc,constructor,sml,ml,svl,VariableList)); ref static_scope(new list_scope); static_scope->unite(ctx.LookupScope); static_scope->unite(cls); FOREACH_CONST(first,StaticMethodList,declaration_list) (*first)->evaluate(context(sml,static_scope)); FOREACH_CONST(first,MethodList,declaration_list) (*first)->evaluate(context(ml,ctx.LookupScope)); FOREACH_CONST(first,StaticVariableList,declaration_list) (*first)->evaluate(context(svl,static_scope)); ctx.DeclarationScope->addMember(Identifier,cls); return cls; } EXJS_ADD_CODE_LOCATION } void js_class_declaration::setConstructor(ref decl) { ConstructorDeclaration = decl; } void js_class_declaration::addStaticMethod(ref decl) { StaticMethodList.push_back(decl); } void js_class_declaration::addMethod(ref decl) { MethodList.push_back(decl); } void js_class_declaration::addStaticVariable(ref decl) { StaticVariableList.push_back(decl); } void js_class_declaration::addVariable(ref decl) { VariableList.push_back(decl); }