puerts / backend-quickjs Goto Github PK
View Code? Open in Web Editor NEWLicense: BSD 3-Clause "New" or "Revised" License
License: BSD 3-Clause "New" or "Revised" License
之前的逃逸有两个问题:
1、如果在回调中,直接用Object把quickjs传来的值作为对象属性值设置给另外一个对象,会崩溃(目前unity和unreal倒无该场景);
2、链式操作访问属性,回收过快的问题,至少导致两个比较让人困惑的问题
Tencent/puerts#118
Tencent/puerts#780
在UE的编辑器下,插件和Game是不同的dll。而quickjs后端有全局变量,静态链接后(插件和Game)都有一份。
这导致在Game模块下直接调用(仿)v8的api有可能崩溃。
上述问题可通过提供dll版本解决。
dll版本主要有几个调整
1、编译的调整;
2、类静态变量的访问放到v8-impl.cc,否则链接说找不到符号;
3、针对ue特有的,重载new和delete的调整:把context的new移动到v8-impl.cc,否则new时用的是ue的内存分配器,delete(Global<T>::Reset()
调用的delete)用的是c++原生delete,会崩溃。
actions突然编译不了,定位发现是msys2官网升级了cmake,回退到cmake-3.23.2-1( 163e2d8 )后可以编译成功。
进一步定位发现可能是该版本cmake在msys2(mingw32-make)上的bug,build\CMakeFiles\quickjs.dir\build.make文件的差别是cmake-3.23.2-1用相对路径,cmake-3.25.1-1用绝对路径
<<< CMakeFiles/quickjs.dir/quickjs/cutils.c.o: ../quickjs/cutils.c
>>> CMakeFiles/quickjs.dir/quickjs/cutils.c.o: /e/backend-quickjs/quickjs/cutils.c
手工改这文件也可以成功编译。
看CMakeList.txt的源码加的应该是没啥问题的。
如下代码被require可以重现
"use strict";
class TsTestActor {
set;
}
static void Print(const v8::FunctionCallbackInfo<v8::Value>& info) {
//std::cout << "Print:" << info.Length() << std::endl;
v8::Isolate* isolate = info.GetIsolate();
v8::Local<v8::Context> context = isolate->GetCurrentContext();
//std::cout << "len:" << info.Length() << std::endl;
auto str = info[0]->ToString(context).ToLocalChecked();
auto len = str->Utf8Length(isolate);
//std::cout << "len:" << len << std::endl;
UE_LOG(LogTemp, Warning, TEXT("len: %d"), len);
char* data = new char[len + 1];
data[len] = 111;
str->WriteUtf8(isolate, data);
//std::cout << "data[n]:" << (int)data[len] << std::endl;
UE_LOG(LogTemp, Warning, TEXT("data[n]: %d"), (int)data[len]);
std::string msg = *(v8::String::Utf8Value(isolate, str));
//std::cout << msg << std::endl;
}
如上代码,用typescirpt调用
globalThis.Print('hello');
用v8时打印
LogTemp: Warning: len: 5
LogTemp: Warning: data[n]: 0
用backend-quickjs时打印
LogTemp: Warning: len: 5
LogTemp: Warning: data[n]: 111
简单来说v8会认为内存分配为Utf8Length + 1,并自动在str[Utf8Length] 添加0结尾。
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.