diff --git a/TinyJS.cpp b/TinyJS.cpp index 99f0c64..c1aa3fc 100755 --- a/TinyJS.cpp +++ b/TinyJS.cpp @@ -95,6 +95,8 @@ Version 0.29 : Added new object via functions Fixed getString() for double on some platforms Version 0.30 : Rlyeh Mario's patch for Math Functions on VC++ + Version 0.31 : Add exec() to TinyJS functions + Now print quoted JSON that can be read by PHP/Python parsers NOTE: Constructing an array with an initial length 'Array(5)' doesn't work @@ -106,6 +108,7 @@ TODO: Utility va-args style function in TinyJS for executing a function directly + Merge the parsing of expressions/statements so eval("statement") works like we'd expect. */ @@ -1212,10 +1215,7 @@ void CScriptVar::getJSON(ostringstream &destination, const string linePrefix) { CScriptVarLink *link = firstChild; while (link) { destination << indentedLinePrefix; - if (isAlphaNum(link->name)) - destination << link->name; - else - destination << getJSString(link->name); + destination << getJSString(link->name); destination << " : "; link->var->getJSON(destination, indentedLinePrefix); link = link->nextSibling; diff --git a/TinyJS_Functions.cpp b/TinyJS_Functions.cpp index 9f30369..ded14f0 100755 --- a/TinyJS_Functions.cpp +++ b/TinyJS_Functions.cpp @@ -148,6 +148,12 @@ void scJSONStringify(CScriptVar *c, void *) { c->getReturnVar()->setString(result.str()); } +void scExec(CScriptVar *c, void *data) { + CTinyJS *tinyJS = (CTinyJS *)data; + std::string str = c->getParameter("jsCode")->getString(); + tinyJS->execute(str); +} + void scEval(CScriptVar *c, void *data) { CTinyJS *tinyJS = (CTinyJS *)data; std::string str = c->getParameter("jsCode")->getString(); @@ -212,7 +218,8 @@ void scArrayJoin(CScriptVar *c, void *data) { // ----------------------------------------------- Register Functions void registerFunctions(CTinyJS *tinyJS) { - tinyJS->addNative("function eval(jsCode)", scEval, tinyJS); // execute the given string and return the result + tinyJS->addNative("function exec(jsCode)", scExec, tinyJS); // execute the given code + tinyJS->addNative("function eval(jsCode)", scEval, tinyJS); // execute the given string (an expression) and return the result tinyJS->addNative("function trace()", scTrace, tinyJS); tinyJS->addNative("function Object.dump()", scObjectDump, 0); tinyJS->addNative("function Object.clone()", scObjectClone, 0);