提交 64780ac1 authored 作者: Mike Jerris's avatar Mike Jerris

Merge pull request #1334 in FS/freeswitch from…

Merge pull request #1334 in FS/freeswitch from ~ANDYWOLK/freeswitch:feature/FS-10496-speedup-javascript-enabling-code to master

* commit 'c1280938':
  FS-10496: [mod_v8] Speedup JavaScript. Enabling Code Caching.
<configuration name="v8.conf" description="Google V8 JavaScript Plug-Ins">
<settings>
<!-- <param name="script-caching" value="enabled"/> -->
<!-- <param name="cache-expires-sec" value="3600"/> -->
<!-- <param name="startup-script" value="startup1.js"/> -->
<!-- <param name="startup-script" value="startup2.js"/> -->
<!-- <param name="xml-handler-script" value="directory.js"/> -->
......
<configuration name="v8.conf" description="Google V8 JavaScript Plug-Ins">
<settings>
<!-- <param name="script-caching" value="enabled"/> -->
<!-- <param name="cache-expires-sec" value="3600"/> -->
<!-- <param name="startup-script" value="startup1.js"/> -->
<!-- <param name="startup-script" value="startup2.js"/> -->
<!-- <param name="xml-handler-script" value="directory.js"/> -->
......
<configuration name="v8.conf" description="Google V8 JavaScript Plug-Ins">
<settings>
<!-- <param name="script-caching" value="enabled"/> -->
<!-- <param name="cache-expires-sec" value="3600"/> -->
<!-- <param name="startup-script" value="startup1.js"/> -->
<!-- <param name="startup-script" value="startup2.js"/> -->
<!-- <param name="xml-handler-script" value="directory.js"/> -->
......
<configuration name="v8.conf" description="Google V8 JavaScript Plug-Ins">
<settings>
<!-- <param name="script-caching" value="enabled"/> -->
<!-- <param name="cache-expires-sec" value="3600"/> -->
<!-- <param name="startup-script" value="startup1.js"/> -->
<!-- <param name="startup-script" value="startup2.js"/> -->
<!-- <param name="xml-handler-script" value="directory.js"/> -->
......
......@@ -36,6 +36,10 @@
#include "javascript.hpp"
#include <switch.h>
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
void LoadScript(v8::MaybeLocal<v8::Script> *v8_script, v8::Isolate *isolate, const char *script_data, const char *script_file);
#endif
SWITCH_BEGIN_EXTERN_C
#define JS_BUFFER_SIZE 1024 * 32
......
......@@ -477,13 +477,20 @@ JS_GLOBAL_FUNCTION_IMPL_STATIC(Include)
string js_file = JSMain::LoadFileToString(script_name);
if (js_file.length() > 0) {
Handle<String> source = String::NewFromUtf8(info.GetIsolate(), js_file.c_str());
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
Handle<Script> script = Script::Compile(source, info[i]->ToString());
MaybeLocal<v8::Script> script;
LoadScript(&script, info.GetIsolate(), js_file.c_str(), script_name);
if (script.IsEmpty()) {
info.GetReturnValue().Set(false);
} else {
info.GetReturnValue().Set(script.ToLocalChecked()->Run());
}
#else
Handle<String> source = String::NewFromUtf8(info.GetIsolate(), js_file.c_str());
Handle<Script> script = Script::Compile(source, info[i]);
#endif
info.GetReturnValue().Set(script->Run());
#endif
switch_safe_free(path);
return;
}
......
......@@ -29,6 +29,9 @@
*/
#include "javascript.hpp"
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
#include "mod_v8.h"
#endif
#ifdef V8_ENABLE_DEBUGGING
#include <v8-debug.h>
......@@ -228,15 +231,21 @@ void JSMain::Include(const v8::FunctionCallbackInfo<Value>& args)
string js_file = LoadFileToString(js_safe_str(*str));
if (js_file.length() > 0) {
Handle<String> source = String::NewFromUtf8(args.GetIsolate(), js_file.c_str());
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
Handle<Script> script = Script::Compile(source, args[i]->ToString());
MaybeLocal<v8::Script> script;
LoadScript(&script, args.GetIsolate(), js_file.c_str(), js_safe_str(*str));
if (script.IsEmpty()) {
args.GetReturnValue().Set(false);
}
else {
args.GetReturnValue().Set(script.ToLocalChecked()->Run());
}
#else
Handle<String> source = String::NewFromUtf8(args.GetIsolate(), js_file.c_str());
Handle<Script> script = Script::Compile(source, args[i]);
#endif
args.GetReturnValue().Set(script->Run());
#endif
return;
}
......@@ -314,15 +323,16 @@ const string JSMain::ExecuteString(const string& scriptData, const string& fileN
inst->obj->RegisterInstance(isolate, inst->name, inst->auto_destroy);
}
// Create a string containing the JavaScript source code.
Handle<String> source = String::NewFromUtf8(isolate, scriptData.c_str());
TryCatch try_catch;
// Compile the source code.
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
Handle<Script> script = Script::Compile(source, String::NewFromUtf8(isolate, fileName.c_str()));
// Compile the source code.
MaybeLocal<v8::Script> script;
LoadScript(&script, isolate, scriptData.c_str(), fileName.c_str());
#else
// Create a string containing the JavaScript source code.
Handle<String> source = String::NewFromUtf8(isolate, scriptData.c_str());
Handle<Script> script = Script::Compile(source, Local<Value>::New(isolate, String::NewFromUtf8(isolate, fileName.c_str())));
#endif
......@@ -330,8 +340,17 @@ const string JSMain::ExecuteString(const string& scriptData, const string& fileN
res = JSMain::GetExceptionInfo(isolate, &try_catch);
isError = true;
} else {
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
// Run the script
Handle<Value> result;
if (!script.IsEmpty()) {
result = script.ToLocalChecked()->Run();
}
#else
// Run the script
Handle<Value> result = script->Run();
#endif
if (try_catch.HasCaught()) {
res = JSMain::GetExceptionInfo(isolate, &try_catch);
isError = true;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论