Como você inclui outro arquivo JS no V8 do Google?
-
18-09-2019 - |
Pergunta
Como você inclui outro arquivo de script dentro de um arquivo de script .js no V8?
Lá está ou003Cscript> tag in HTML but how can it be done inside a v8 embedded program?
Solução
Você tem que adicionar essa funcionalidade manualmente, eis como eu fiz:
Handle<Value> Include(const Arguments& args) {
for (int i = 0; i < args.Length(); i++) {
String::Utf8Value str(args[i]);
// load_file loads the file with this name into a string,
// I imagine you can write a function to do this :)
std::string js_file = load_file(*str);
if(js_file.length() > 0) {
Handle<String> source = String::New(js_file.c_str());
Handle<Script> script = Script::Compile(source);
return script->Run();
}
}
return Undefined();
}
Handle<ObjectTemplate> global = ObjectTemplate::New();
global->Set(String::New("include"), FunctionTemplate::New(Include));
Ele basicamente adiciona uma função acessível globalmente que pode carregar e executar um arquivo JavaScript no contexto atual. Eu o uso com meu projeto, funciona como um sonho.
// beginning of main javascript file
include("otherlib.js");
Outras dicas
Se você estiver usando o Node.js ou qualquer tempo de execução compatível com o Commonsjs, poderá usar o exigir (módulo); Há um bom artigo sobre isso em http://jherdman.ca/2010-04-05/undestanding-nodejs-require/
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow