tencent / scriptx Goto Github PK
View Code? Open in Web Editor NEWA versatile script engine abstraction layer.
License: Other
A versatile script engine abstraction layer.
License: Other
跪求 😭
Hi,
When running the following code with QuickJs backend an exception is thrown
const promise = new Promise((resolve, reject) => {
setTimeout(() => { resolve('Ok') }, 300);
});
promise.then(x => { console.log(x); }); // Exception gets thrown here
The exception is about the missing engine scope, thrown from QjsEngine::newPrototype
newRawFunction
lambda. This is because when executing the promise_reaction_job
(that is the .then()
part) and eventually the lambda function created in QjsEngine::newPrototype
, the engine scope is not set (the promise resolution is called on the message loop) so the args.thiz()
in newRawFunction
throws an exception.
It seems this can be solved by creating a scope in there:
// QjsEngine.hpp
// QjsEngine::newPrototype
auto fun = newRawFunction(
this, const_cast<FuncDef*>(&f), definePtr,
[](const Arguments& args, void* data1, void* data2, bool) {
EngineScope scope(args.engine()); // <---------------------------- *HERE*
auto ptr = static_cast<InstanceClassOpaque*>(
JS_GetOpaque(qjs_interop::peekLocal(args.thiz()), kInstanceClassId));
if (ptr == nullptr || ptr->classDefine != data2) {
throw Exception(u8"call function on wrong receiver");
}
auto f = static_cast<FuncDef*>(data1);
Tracer tracer(args.engine(), f->traceName);
return (f->callback)(static_cast<T*>(ptr->scriptClassPolymorphicPointer), args);
});
请问如何配置python单元测试?
######## ScriptX config ##########
# 1. import ScriptX
# set which backend engine to use
set(SCRIPTX_BACKEND Python CACHE STRING "" FORCE)
我将test文件夹的cmake改成这样
结果编译时仍有v8的引用
配置cmake时我发现有仓库的克隆
但是文件里面我并没有发现git链接
就感觉很奇怪
JSManagedValue 是用来解决循环应用的,并且保证不会被GC
JscWeakRef 是不是类似c++ weak_ptr, 还是会被GC掉?
如题,至今引擎加载源码都只能依靠读取文件然后eval,在适配其他引擎过程中此种加载方式会带来一些困扰
比如
因此有必要为Engine.h提供支持加载指定路径文件的eval,并在不同引擎做特殊处理
开发环境是VS2022 C++20
void exportHostAbility(const std::shared_ptrscript::ScriptEngine& engine) {
using host_ability::HostImage;
// wrapper inherits from HostImage and ScriptClass
class HostImageWrapper : public HostImage, public script::ScriptClass {
public:
using script::ScriptClass::ScriptClass;
};
static script::ClassDefine<HostImageWrapper> hostImageDef =
script::defineClass<HostImageWrapper>("Image")
.constructor()
.instanceProperty("src", &HostImage::getSrc, &HostImage::setSrc)
.instanceProperty("width", &HostImage::getWidth, nullptr)
.instanceProperty("height", &HostImage::getHeight, nullptr)
.instanceFunction("drop", &HostImage::drop)
.build();
engine->registerNativeClass(hostImageDef);
auto drawImageFunc =
script::Function::newFunction([](HostImageWrapper* img) { host_ability::drawImage(img); });
engine->set("_drawImage", drawImageFunc);
auto sendMessageFunc = script::Function::newFunction(host_ability::sendMessage);
engine->set("_sendMessage", sendMessageFunc);
}
auto engine = createEngine();
// 1. export function
{
script::EngineScope enter(engine.get());
try {
exportHostAbility(engine);//这里异常了
}
catch (const script::Exception& e) {
std::cerr << "failed to run game " << e;
}
}
// 2. inject host base lib
{
script::EngineScope enter(engine.get());
try {
engine->eval(getScriptBaseLibrary());
}
catch (const script::Exception& e) {
std::cerr << "failed to run game " << e;
}
}
// 3. run downloaded game script
{
script::EngineScope enter(engine.get());
try {
engine->eval(downloadGameScript());
}
catch (const script::Exception& e) {
std::cerr << "failed to run game " << e;
}
}
// exit and shutdown.
Hi,
When compiled with QuickJs backend I experience memory leaks reported by MSVC compiler.
I use std::shared_ptr<script::ScriptEngine>
with provided script::ScriptEngine::Deleter()
.
One problem is inside QjsEngine::destroy()
which is missing the delete this;
statement.
And another one is in MessageQueue.cc
because of the global static runningQueue_
.
Could be a false-positive, but it can be fixed by moving the runningQueue_
inside the getRunningQueue()
function:
using RunnineQueueMap = std::unordered_map<MessageQueue*, int>;
static inline RunnineQueueMap& getRunningQueue() {
SCRIPTX_THREAD_LOCAL(RunnineQueueMap, runningQueue_);
return internal::getThreadLocal(runningQueue_);
}
Thanks!
Great project!
When I try to import ScriptX into my project, I follow the ScriptX cmake guide:
set(SCRIPTX_BACKEND QuickJs)
include(ScriptX/CMakeLists.txt)
However, I get an error the first time I run cmake:
CMake Error at ScriptX/CMakeLists.txt:81 (message):
Platform not set. Please set SCRIPTX_BACKEND. Candidates:
V8;JavaScriptCore;SpiderMonkey;QuickJs;WebAssembly;WKWebView;Lua;Python;Ruby;Empty
If I run cmake again, it works now. 🧐
This seems to be because line 42 of CMakeLists.txt resets the variable to a blank string the first time it is run.
You can see this by adding some logging:
message("backend: [${SCRIPTX_BACKEND}]") # backend: [QuickJs]
set(SCRIPTX_BACKEND "" CACHE STRING "choose which ScriptX Backend(Implementation) to use, V8 or JavaScriptCore or etc...")
message("backend: [${SCRIPTX_BACKEND}]") # backend: []
According to the documentation, this is expected if policy CMP0126 is set to OLD
.
That policy was introduced in version 3.21. However, since ScriptX does cmake_minimum_required(VERSION 3.0)
, this policy is set to the OLD
behavior (see here).
If you are using cmake 3.21 or newer, you can set the policy explicitly:
# add this line first:
set(CMAKE_POLICY_DEFAULT_CMP0126 NEW)
# ...now we can do these lines:
set(SCRIPTX_BACKEND QuickJs)
include(ScriptX/CMakeLists.txt)
If you are running an older version, you can copy line 42 into your file and change it, like this:
set(SCRIPTX_BACKEND QuickJs CACHE STRING "choose which ScriptX Backend(Implementation) to use, V8 or JavaScriptCore or etc...")
include(ScriptX/CMakeLists.txt)
It seems there are a few options.
set(SCRIPTX_BACKEND "${SCRIPTX_BACKEND}" CACHE STRING "choose which ScriptX Backend(Implementation) to use, V8 or JavaScriptCore or etc...")
Option 1 works in my tests and seems like the cleanest fix, but I'm not sure what is best.
ScriptX is a really impressive library! It's well-designed and well-engineered, and promises to a be a huge benefit to my project. Thank you for making it 🎉
尽管说Js引擎内部统一使用64位浮点数来表示数字,但是Local无法区分类型这个情况还是经常导致ScriptX数据类型在转换到C++类型时出现一定的问题,特别是做ScriptX和Json类型互转的过程当中不知该如何处理整数 / 浮点数相关的东西
请问如果使用v8 on windows 是要单独编译下v8吗
cmake 的项目 看文档中还要 libv8.a
关于引入到项目中的文档 可以再详细点吗 门槛感觉有点高
问题描述:把小游戏demo单独提取出来,选择V8为backend,把test自动从github拉取的那个作者编译好的v8提取出来,加入项目并链接。接下来启动demo的时候报错
#
# Fatal error in , line 0
# Failed to deserialize the V8 snapshot blob. This can mean that the snapshot blob file is corrupted or missing.
#
#
#
#FailureMessage Object: 0000005F167AF088
看起来像是V8快照加载失败
不知该如何解决
另外问一下v8该如何编译才能符合作为scriptx后端的要求
const image = new Image(60, 45); // Using optional size for image
image.onload = function() {
canvas.draw(image)
};
image.src = 'https://mdn.mozillademos.org/files/5397/rhino.jpg';
如果Image是c++实现的,就会出现内存泄漏吧
lua原生require可以正常使用。但在V8 Js测试 ESM 时提示
Uncaught SyntaxError: Cannot use import statement outside a module
原生V8支持ESM需要通过.mjs后缀,但是由于ScriptX是基于eval的加载,没法让引擎知道当前文件需要支持ESM
貌似QuickJs就原生支持了ESM,ScriptX可以在V8这边做一些处理吗
本来打算在同一次构建中对每种语言分别生成一个Target的输出
每次include( ScriptX/CMakeLists.txt )前先修改SCRIPTX_BACKEND
结果cmake报错
CMake Error at src/ScriptX/CMakeLists.txt:50 (add_library):
[cmake] add_library cannot create target "ScriptX" because another target with the
[cmake] same name already exists. The existing target is a static library created
[cmake] in source directory
也就是说在同一次构建中不能有两个同名对象
那该如何实现同时构建多种语言的多个target呢
或者有无办法实现构建完一个target后执行clean
编译正确无警告,使用的是你预编译的V8,在单独的exe项目工作正常,,到作为DLL加载时就出问题了
不知有无遇到过这样的问题
经测试lua版dll运行正常。
由于目前没找到编译玩的x64的jscore或者quickjs所以只能搞v8。。。或者不知可否发一下您precompile完测试用的jsc或qjs(泪目)
scriptDynamicCast参数类型:
R: V8Engine
T: ScriptEngine
scriptDynamicCast里测试代码来看:
请问是否scriptDynamicCast的实现在向下类型转换时有问题?
Add PHP support please
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.