I am currently using v8 as my javascript engine in order to calculate
conceptual representations of natural language input. It works
extremely well, but I feel that it could have better performance.
In my snippet of code lower, I first compile the javascript source,
and then execute it. I have tried to keep the script variable into a
cache so that I wouldn't need to compile it on the second pass, but it
appears the script variable only lives within the context under which
it was compiled. This is a sad limitation that I would like to get rid
of. Is there a way to do that?
Handle<Value> ExecuteJavascriptString(Persistent<Context> context,
string dSourceStr, string nameStr, bool skiptrace) throw
(CMathematicalError)
{
// Enter the created context for compiling and
// running the hello world script.
Context::Scope context_scope(context);
Handle<Value> result;
Handle<String> source = String::New(dSourceStr.c_str());
Handle<Script> script;
Handle<Value> name = String::New(nameStr.c_str());
{
TryCatch try_catch;
script = Script::Compile(source, name);
if (script.IsEmpty())
{
String::AsciiValue error(try_catch.Exception());
string tmpString = *error;
throw(new CMathematicalError(tmpString));
}
}
string indent = "";
if ((!skiptrace) && (DigitalConceptBuilder::GetCurCalculationContext
()->GetJavascriptTrace()))
{
for (int i = 0; i < g_alreadyReporting; i++)
{
indent += "\t";
}
Handle<Value> dCurPredicate = ExecuteJavascriptString(context,
"var
curPredicate; curPredicate", "trace", true);
string dCurPredicateStr = ProcessValueToDisplay(dCurPredicate,
false);
Handle<Value> dCurNode = ExecuteJavascriptString(context, "var
curNode; curNode", "trace", true);
string dCurNodeStr = ProcessValueToDisplay(dCurNode, false);
printf("%s %s %s %s\n%s[\n", g_nodeContext.empty()?"":(indent +
"NODE:" + g_nodeContext.top()->GetNodeDesc() + ((g_nodeContext.top()-
>GetConstructionLine() != "")?(", " + g_nodeContext.top()-
>GetConstructionLine()):"")).c_str(), g_sourceContext.empty()?"":
(g_nodeContext.empty()?"":"\n" + indent + "REF:\"" +
g_sourceContext.top() + "\"").c_str(), ("\n" + indent + "curPredicate:
" + dCurPredicateStr).c_str(), ("\n" + indent + "curNode: " +
dCurNodeStr).c_str(), indent.c_str());
SearchAndReplace(dSourceStr, "\n", "\1");
SearchAndReplace(dSourceStr, "\1", "\n\t" + indent);
SearchAndReplace(dSourceStr, "\r\n", "\1");
SearchAndReplace(dSourceStr, "\1", "\n");
printf("%s\t%s", indent.c_str(), dSourceStr.c_str());
printf("\n");
}
g_alreadyReporting++;
result = script->Run();
g_alreadyReporting--;
if ((!skiptrace) && (DigitalConceptBuilder::GetCurCalculationContext
()->GetJavascriptTrace()))
{
string dDisplay = ProcessValueToDisplay(result, true);
SearchAndReplace(dDisplay, "\n", "\1");
SearchAndReplace(dDisplay, "\1", "\n" + indent);
Handle<Value> dCurPredicate = ExecuteJavascriptString(context,
"var
curPredicate; curPredicate", "trace", true);
string dCurPredicateStr = ProcessValueToDisplay(dCurPredicate,
false);
printf("%s] -> %s, curPredicate -> %s\n\n", indent.c_str(),
dDisplay.c_str(), dCurPredicateStr.c_str());
}
return result;
}
--
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users