Giter Club home page Giter Club logo

scriptx's People

Contributors

jasonzyt avatar jimmy54 avatar jrpat avatar landerlyoung avatar ryecrow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

scriptx's Issues

Local<Array>'s behavior in lua may be wrong

My test code is :
image

And the result is:
image

It is clear that in the test code, arr is an array in lua.
It seems a bit confusing since the .isArray() returns True while .kind() == ValueKind::kArray returns False

Memory leak in QuickJs backend

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!

是否可以给Local<Number>加上区分整数 / 浮点数的能力

尽管说Js引擎内部统一使用64位浮点数来表示数字,但是Local无法区分类型这个情况还是经常导致ScriptX数据类型在转换到C++类型时出现一定的问题,特别是做ScriptX和Json类型互转的过程当中不知该如何处理整数 / 浮点数相关的东西

V8引擎作为DLL嵌入工作时出错

编译正确无警告,使用的是你预编译的V8,在单独的exe项目工作正常,,到作为DLL加载时就出问题了
不知有无遇到过这样的问题
经测试lua版dll运行正常。

由于目前没找到编译玩的x64的jscore或者quickjs所以只能搞v8。。。或者不知可否发一下您precompile完测试用的jsc或qjs(泪目)

Add new APIs to "eval a file" in Engine.h

如题,至今引擎加载源码都只能依靠读取文件然后eval,在适配其他引擎过程中此种加载方式会带来一些困扰
比如

  • V8 engine 和 embedded NodeJs 需要使用.mjs后缀判定此文件为ES Module文件,以启动ES Module支持
  • Python解释器用import可以更方便地实现实例隔离,而非使用其标准中尚不太完善的subinterpreter
  • 等等

因此有必要为Engine.h提供支持加载指定路径文件的eval,并在不同引擎做特殊处理

scriptDynamicCast返回NULL导致空指针崩溃

调用栈:
image

  • Engine.hpp:39 是把引擎类型从ScriptEngine向下转型为V8Engine, scriptDynamicCast返回了NULL;
  • 工程开启了rtti, scriptDynamicCast的实现使用了dynamic_cast;

scriptDynamicCast参数类型:
R: V8Engine
T: ScriptEngine

scriptDynamicCast里测试代码来看:

  • 使用static_cast 转换时t_static_cast非空;
  • 使用dynamic_cast 转换时t_forward_dynamic_cast 为空;

请问是否scriptDynamicCast的实现在向下类型转换时有问题?

SCRIPTX_BACKEND gets erased by ScriptX/CMakeLists.txt

Environment

  • cmake: version 3.22.1
  • ScriptX: latest master branch

Problem

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. 🧐

Reason

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).

Workaround

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)

Solution

It seems there are a few options.

  1. Change line 42 to make it capture the user variable:
    set(SCRIPTX_BACKEND "${SCRIPTX_BACKEND}" CACHE STRING "choose which ScriptX Backend(Implementation) to use, V8 or JavaScriptCore or etc...")
    
  2. Change the cmake guide to recommend the workarounds above.
  3. Change the minimum required version to 3.21. I'm not sure if this is desirable for the project, since some users may be using older versions.

Option 1 works in my tests and seems like the cleanest fix, but I'm not sure what is best.

Thanks

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 🎉

Python单元测试

请问如何配置python单元测试?

######## ScriptX config ##########

# 1. import ScriptX
#   set which backend engine to use
set(SCRIPTX_BACKEND Python CACHE STRING "" FORCE)

我将test文件夹的cmake改成这样
结果编译时仍有v8的引用

配置cmake时我发现有仓库的克隆
但是文件里面我并没有发现git链接
就感觉很奇怪

Exception when using promises with QuickJs backend

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);
        });

demo代码运行异常.failed to run game can't cast value as Object [failed to obtain stacktrace]

开发环境是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.

在同一次构建中更换编译开关重复构建ScriptX时报错

本来打算在同一次构建中对每种语言分别生成一个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作为后端启动时遇到问题

问题描述:把小游戏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后端的要求

希望可以增加对 Js ESM 的支持

lua原生require可以正常使用。但在V8 Js测试 ESM 时提示

Uncaught SyntaxError: Cannot use import statement outside a module

原生V8支持ESM需要通过.mjs后缀,但是由于ScriptX是基于eval的加载,没法让引擎知道当前文件需要支持ESM
貌似QuickJs就原生支持了ESM,ScriptX可以在V8这边做一些处理吗

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.