Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QuickJs support ESM and add engine->loadFile #91

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions backend/JavaScriptCore/JscEngine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "../../src/Native.hpp"
#include "JscEngine.hpp"
#include "JscHelper.h"
#include "../../src/utils/Helper.hpp"

namespace script::jsc_backend {

Expand Down Expand Up @@ -177,6 +178,27 @@ script::Local<script::Value> JscEngine::eval(const script::Local<script::String>
return eval(script, {});
}

Local<Value> JscEngine::loadFile(const Local<String>& scriptFile) {
if(scriptFile.toString().empty())
throw Exception("script file no found");
Local<Value> content = internal::readAllFileContent(scriptFile);
if(content.isNull())
throw Exception("can't load script file");

std::string sourceFilePath = scriptFile.toString();
std::size_t pathSymbol = sourceFilePath.rfind("/");
if(pathSymbol != -1)
sourceFilePath = sourceFilePath.substr(pathSymbol + 1);
else
{
pathSymbol = sourceFilePath.rfind("\\");
if(pathSymbol != -1)
sourceFilePath = sourceFilePath.substr(pathSymbol + 1);
}
Local<String> sourceFileName = String::newString(sourceFilePath);
return eval(content.asString(), sourceFileName);
}

std::shared_ptr<utils::MessageQueue> JscEngine::messageQueue() { return messageQueue_; }

void JscEngine::gc() {
Expand Down
2 changes: 2 additions & 0 deletions backend/JavaScriptCore/JscEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class JscEngine : public ::script::ScriptEngine {
Local<Value> eval(const Local<String>& script) override;
using ScriptEngine::eval;

Local<Value> loadFile(const Local<String>& scriptFile) override;

std::shared_ptr<utils::MessageQueue> messageQueue() override;

void gc() override;
Expand Down
22 changes: 22 additions & 0 deletions backend/Lua/LuaEngine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "LuaHelper.hpp"
#include "LuaReference.hpp"
#include "LuaScope.hpp"
#include "../../src/utils/Helper.hpp"

// ref https://www.lua.org/manual/5.1/manual.html
// https://www.lua.org/wshop14/Zykov.pdf
Expand Down Expand Up @@ -259,6 +260,27 @@ Local<Value> LuaEngine::eval(const Local<String>& script, const Local<Value>& so
return lua_backend::callFunction({}, {}, 0, nullptr);
}

Local<Value> LuaEngine::loadFile(const Local<String>& scriptFile) {
if(scriptFile.toString().empty())
throw Exception("script file no found");
Local<Value> content = internal::readAllFileContent(scriptFile);
if(content.isNull())
throw Exception("can't load script file");

std::string sourceFilePath = scriptFile.toString();
std::size_t pathSymbol = sourceFilePath.rfind("/");
if(pathSymbol != -1)
sourceFilePath = sourceFilePath.substr(pathSymbol + 1);
else
{
pathSymbol = sourceFilePath.rfind("\\");
if(pathSymbol != -1)
sourceFilePath = sourceFilePath.substr(pathSymbol + 1);
}
Local<String> sourceFileName = String::newString(sourceFilePath);
return eval(content.asString(), sourceFileName);
}

Arguments LuaEngine::makeArguments(LuaEngine* engine, int stackBase, size_t paramCount,
bool isInstanceFunc) {
lua_backend::ArgumentsData argumentsData{engine, stackBase, paramCount, isInstanceFunc};
Expand Down
2 changes: 2 additions & 0 deletions backend/Lua/LuaEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class LuaEngine : public ScriptEngine {
Local<Value> eval(const Local<String>& script) override;
using ScriptEngine::eval;

Local<Value> loadFile(const Local<String>& scriptFile) override;

std::shared_ptr<utils::MessageQueue> messageQueue() override;

void gc() override;
Expand Down
37 changes: 37 additions & 0 deletions backend/QuickJs/QjsEngine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "QjsEngine.h"
#include <ScriptX/ScriptX.h>
#include "../../src/utils/Helper.hpp"

namespace script::qjs_backend {

Expand Down Expand Up @@ -94,6 +95,9 @@ QjsEngine::QjsEngine(std::shared_ptr<utils::MessageQueue> queue, const QjsFactor
}

initEngineResource();

/* set default loader for ES6 modules */
JS_SetModuleLoaderFunc(runtime_, NULL, js_module_loader, NULL);
}

void QjsEngine::initEngineResource() {
Expand Down Expand Up @@ -268,6 +272,39 @@ Local<Value> QjsEngine::eval(const Local<String>& script, const Local<Value>& so
return Local<Value>(ret);
}

Local<Value> QjsEngine::loadFile(const Local<String>& scriptFile) {
Tracer trace(this, "QjsEngine::loadFile");

if(scriptFile.toString().empty())
throw Exception("script file no found");
Local<Value> content = internal::readAllFileContent(scriptFile);
if(content.isNull())
throw Exception("can't load script file");

// get source file name
std::string sourceFilePath = scriptFile.toString();
std::size_t pathSymbol = sourceFilePath.rfind("/");
if(pathSymbol != -1)
sourceFilePath = sourceFilePath.substr(pathSymbol + 1);
else
{
pathSymbol = sourceFilePath.rfind("\\");
if(pathSymbol != -1)
sourceFilePath = sourceFilePath.substr(pathSymbol + 1);
}
Local<String> sourceFileName = String::newString(sourceFilePath);

StringHolder contentStr(content.asString());
StringHolder fileNameStr(sourceFileName);
JSValue ret = JS_Eval(context_, contentStr.c_str(), contentStr.length(), fileNameStr.c_str(),
JS_EVAL_TYPE_MODULE);

qjs_backend::checkException(ret);
scheduleTick();

return Local<Value>(ret);
}

std::shared_ptr<utils::MessageQueue> QjsEngine::messageQueue() { return queue_; }

void QjsEngine::gc() {
Expand Down
2 changes: 2 additions & 0 deletions backend/QuickJs/QjsEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class QjsEngine : public ScriptEngine {
Local<Value> eval(const Local<String>& script) override;
using ScriptEngine::eval;

Local<Value> loadFile(const Local<String>& scriptFile) override;

std::shared_ptr<utils::MessageQueue> messageQueue() override;

void gc() override;
Expand Down
1 change: 1 addition & 0 deletions backend/QuickJs/QjsHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

SCRIPTX_BEGIN_INCLUDE_LIBRARY
#include <quickjs.h>
#include <quickjs-libc.h>
SCRIPTX_END_INCLUDE_LIBRARY

namespace script::qjs_backend {
Expand Down
22 changes: 22 additions & 0 deletions backend/V8/V8Engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <ScriptX/ScriptX.h>
#include <cassert>
#include <memory>
#include "../../src/utils/Helper.hpp"

namespace script::v8_backend {

Expand Down Expand Up @@ -174,6 +175,27 @@ Local<Value> V8Engine::eval(const Local<String>& script, const Local<String>& so

Local<Value> V8Engine::eval(const Local<String>& script) { return eval(script, {}); }

Local<Value> V8Engine::loadFile(const Local<String>& scriptFile) {
if(scriptFile.toString().empty())
throw Exception("script file no found");
Local<Value> content = internal::readAllFileContent(scriptFile);
if(content.isNull())
throw Exception("can't load script file");

std::string sourceFilePath = scriptFile.toString();
std::size_t pathSymbol = sourceFilePath.rfind("/");
if(pathSymbol != -1)
sourceFilePath = sourceFilePath.substr(pathSymbol + 1);
else
{
pathSymbol = sourceFilePath.rfind("\\");
if(pathSymbol != -1)
sourceFilePath = sourceFilePath.substr(pathSymbol + 1);
}
Local<String> sourceFileName = String::newString(sourceFilePath);
return eval(content.asString(), sourceFileName);
}

void V8Engine::registerNativeClassStatic(v8::Local<v8::FunctionTemplate> funcT,
const internal::StaticDefine* staticDefine) {
for (auto& prop : staticDefine->properties) {
Expand Down
2 changes: 2 additions & 0 deletions backend/V8/V8Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ class V8Engine : public ::script::ScriptEngine {
Local<Value> eval(const Local<String>& script) override;
using ScriptEngine::eval;

Local<Value> loadFile(const Local<String>& scriptFile) override;

/**
* Create a new V8 Engine that share the same isolate, but with different context.
* Caller own the returned pointer, and the returned instance
Expand Down
22 changes: 22 additions & 0 deletions backend/WebAssembly/WasmEngine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "WasmNative.hpp"
#include "WasmReference.hpp"
#include "WasmScope.hpp"
#include "../../src/utils/Helper.hpp"

namespace script::wasm_backend {

Expand Down Expand Up @@ -76,6 +77,27 @@ Local<Value> WasmEngine::eval(const Local<String>& script, const Local<Value>& s
return Local<Value>(retIndex);
}

Local<Value> WasmEngine::loadFile(const Local<String>& scriptFile) {
if(scriptFile.toString().empty())
throw Exception("script file no found");
Local<Value> content = internal::readAllFileContent(scriptFile);
if(content.isNull())
throw Exception("can't load script file");

std::string sourceFilePath = scriptFile.toString();
std::size_t pathSymbol = sourceFilePath.rfind("/");
if(pathSymbol != -1)
sourceFilePath = sourceFilePath.substr(pathSymbol + 1);
else
{
pathSymbol = sourceFilePath.rfind("\\");
if(pathSymbol != -1)
sourceFilePath = sourceFilePath.substr(pathSymbol + 1);
}
Local<String> sourceFileName = String::newString(sourceFilePath);
return eval(content.asString(), sourceFileName);
}

std::shared_ptr<utils::MessageQueue> WasmEngine::messageQueue() { return messageQueue_; }

void WasmEngine::gc() {}
Expand Down
2 changes: 2 additions & 0 deletions backend/WebAssembly/WasmEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class WasmEngine : public ScriptEngine {
Local<Value> eval(const Local<String>& script) override;
using ScriptEngine::eval;

Local<Value> loadFile(const Local<String>& scriptFile) override;

std::shared_ptr<utils::MessageQueue> messageQueue() override;

void gc() override;
Expand Down
11 changes: 11 additions & 0 deletions src/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ class ScriptEngine {
String::newString(std::forward<R>(sourceFileStringLike)));
}

/**
* @param scriptFile path of script file to load
* @return evaluate result
*/
virtual Local<Value> loadFile(const Local<String>& scriptFile) = 0;

template <typename T = std::string, StringLikeConcept(T)>
Local<Value> loadFile(T&& scriptFileStringLike) {
return loadFile(String::newString(std::forward<T>(scriptFileStringLike)));
}

/**
* register a native class definition (constructor & property & function) to script.
* @tparam T a subclass of the NativeClass, which implements all the Script-Native method in cpp.
Expand Down
14 changes: 14 additions & 0 deletions src/utils/Helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <ScriptX/ScriptX.h>

#include <utility>
#include <fstream>

namespace script::internal {

Expand Down Expand Up @@ -56,4 +57,17 @@ Local<Value> getNamespaceObject(ScriptEngine* engine, const std::string_view& na
return nameSpaceObj;
}

Local<Value> readAllFileContent(const Local<String>& scriptFile)
{
std::ifstream fRead;
fRead.open(scriptFile.toString(), std::ios_base::in);
if (!fRead.is_open()) {
return Local<Value>();
}
std::string data((std::istreambuf_iterator<char>(fRead)),
std::istreambuf_iterator<char>());
fRead.close();
return String::newString(std::move(data)).asValue();
}

} // namespace script::internal
2 changes: 2 additions & 0 deletions src/utils/Helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ void withNArray(size_t N, FN&& fn) {

Local<Value> getNamespaceObject(ScriptEngine* engine, const std::string_view& nameSpace,
Local<Value> rootNs = {});

Local<Value> readAllFileContent(const Local<String>& scriptFile);
} // namespace script::internal