I love to talk on trending ⚡ technology ⚡, stay hungry stay foolish. Connect with me here
You are my visitor, Thank You!🎉🎉
JavaScriptCore for Flutter use dart:ffi.
License: MIT License
近期会添加windows和linux平台支持吗?
我找到一个支持windows、linux、macos的JSC项目 Lichtso/JSC-Standalone,它的头文件和 flutter_jscore/lib/binding 下声明一致,但是这个项目时间太古老,链接库是六年前的了,缺少一些方法。
如果coming soon还要等比较久的话,我准备用这个老版本先凑合用。
How we able call dart async functions from javascript?
Thank you.
我这里创建一个test方法,接收一个回调函数。如果我同步触发这个函数可以正常工作。
如果异步触发就直接崩溃了。
class TestBug2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
child: Text('TEST2'),
onPressed: () {
final jsContext = JSContext.createInGroup();
jsContext.globalObject.setProperty(
'test',
JSObject.makeFunctionWithCallback(
jsContext, 'test',
Pointer.fromFunction(testFunction)
).toValue(),
JSPropertyAttributes.kJSPropertyAttributeDontDelete);
jsContext.evaluate('test((n) => n + n)');
},
)),
);
}
static Pointer testFunction(
Pointer ctx,
Pointer function,
Pointer thisObject,
int argumentCount,
Pointer<Pointer> arguments,
Pointer<Pointer> exception) {
final context = JSContext(ctx);
final jsv = JSValue(context, arguments[0]);
// jsv.protect();
// ⬇️同步执行 没有问题直接输出结果
final data = jsv.toObject().callAsFunction(JSObject(context, thisObject), JSValuePointer(JSValue.makeNumber(context, 2.0).pointer));
print(data.toNumber());
// ⬇️模拟异步执行,直接崩溃
Future.delayed(const Duration(milliseconds: 300), () {
final data = jsv.toObject().callAsFunction(JSObject(context, thisObject), JSValuePointer(JSValue.makeNumber(context, 2.0).pointer));
print(data.toNumber());
});
}
}
F/libc ( 9378): Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0 in tid 9417 (Thread-2), pid 9378 ()
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: 'google/coral/coral:10/QQ2A.200305.003/6156912:user/release-keys'
Revision: 'MP1.0'
ABI: 'arm64'
Timestamp: 2020-03-17 14:29:15+0800
pid: 9378, tid: 9417, name: Thread-2 >>> com.example.jsrun <<<
uid: 10289
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0
Cause: null pointer dereference
x0 0000006ff2a46c98 x1 0000000000000000 x2 0000006fdc7e5b40 x3 0000006ff2a45f40
x4 0000006fcd6c2ee0 x5 0000006fdc7e5b40 x6 0000006fd63515e4 x7 0000006fd4cb48f1
x8 0000006ff2a44000 x9 0000000000000001 x10 0000000000000001 x11 0000007040000000
x12 0000000000000a00 x13 000000000c4ef110 x14 0000000000000068 x15 0000006ff2a46cc0
x16 0000000000000000 x17 0000007051dc0970 x18 0000000000000002 x19 0000006fdc7e5b40
x20 0000000000000000 x21 0000006fcd6c2ee0 x22 0000006ff2a45f40 x23 0000007051d2e2b8
x24 0000006fe2d80041 x25 0000006ff296e000 x26 0000007051dc0400 x27 0000006fe1187010
x28 0000000000000004 x29 0000006ff2a46cd8
sp 0000006ff2a46c80 lr 0000006fd635161c pc 0000006fd6906e8c
backtrace:
#00 pc 0000000000680e8c /data/app/com.example.jsrun-IMKGo_ZTkeXv0aKVB8-Nmw==/lib/arm64/libjsc.so
#01 pc 00000000000cb618 /data/app/com.example.jsrun-IMKGo_ZTkeXv0aKVB8-Nmw==/lib/arm64/libjsc.so (JSValueToObject+52)
#02 pc 000000000000485c <anonymous:6fe2d00000>
Lost connection to device.
Can you give an example of how to throw an exception from inside a JSObjectCallAsFunctionCallback callback? I can't figure it out.
I want to create a function return promise to javascript, but the code only run to print('2') and got error at JSObject.makeDeferredPromise
Pointer _getUser(
Pointer ctx,
Pointer function,
Pointer thisObject,
int argumentCount,
Pointer<Pointer> arguments,
Pointer<Pointer> exception) {
print('getUrl called');
final resolve = JSObject.makeFunction(
_jsContext, 'resolve', JSStringPointer.array(['value']), '', '');
final reject = JSObject.makeFunction(
_jsContext, 'reject', JSStringPointer.array(['value']), '', '');
print('2');
final promise = JSObject.makeDeferredPromise(_jsContext,
JSObjectPointer(resolve.pointer), JSObjectPointer(reject.pointer));
print('3');
return promise.pointer;
}
both version from pub.dev or git got the error
flutter_jscore:
git:
url: https://github.com/xuelongqy/flutter_jscore.git
flutter doctor
[✓] Flutter (Channel stable, 1.22.4, on Mac OS X 10.14.6 darwin-x64,
locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[!] Android Studio (version 4.1)
✗ Flutter plugin not installed; this adds Flutter specific functionality.
✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] VS Code (version 1.51.1)
[✓] Connected device (1 available)
Please update this plugin to support flutter 2 and dart 2.12 and nullsafety
我在dart里创建的类,js里初始化后返回到dart再取值发现取到的都是最后一个
js执行的: [new Text('测试1'), new Text('测试2'), new Text('测试3')]
;
到dart中取到的内容是 (测试3, 测试3, 测试3)
全部变成最后一个了
大佬帮忙看看是不是用的方法不对.
完整代码如下:
class TestBug extends StatelessWidget {
static Pointer jsClassInitialize(
Pointer ctx,
Pointer constructor,
int argumentCount,
Pointer<Pointer> arguments,
Pointer<Pointer> exception) {
String text;
final context = JSContext(ctx);
final that = JSValue(context, constructor).toObject();
if (argumentCount >= 1) {
final arg1 = JSValue(context, arguments[0]);
if (arg1.isString) {
text = arg1.string;
}
}
that.setProperty(
'text',
JSValue.makeString(context, text),
JSPropertyAttributes.kJSPropertyAttributeDontDelete);
return constructor;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
child: Text('TEST'),
onPressed: () {
final jsContext = JSContext.createInGroup();
final classDef = JSClassDefinition(
version: 0,
attributes: JSClassAttributes.kJSClassAttributeNone,
className: 'Text',
callAsConstructor: Pointer.fromFunction(jsClassInitialize),
staticFunctions: [],
);
var flutterJSClass = JSClass.create(classDef);
var flutterJSObject = JSObject.make(jsContext, flutterJSClass);
jsContext.globalObject.setProperty('Text', flutterJSObject.toValue(),
JSPropertyAttributes.kJSPropertyAttributeDontDelete);
final data = jsContext.evaluate("""
[new Text('测试1'), new Text('测试2'), new Text('测试3')]
""");
final list = jsValueToList(data).map((e) => e.toObject().getProperty('text').string);
print(list);
// I/flutter ( 8752): (测试3, 测试3, 测试3)
},
)),
);
}
}
final define = JSClassDefinition(
className: 'DartTest',
staticFunctions: [],
);
final jsobject = JSObject.make(jsContext, JSClass.create(define));
jsContext.globalObject.setProperty(
name,
jsobject.toValue(),
JSPropertyAttributes.kJSPropertyAttributeNone,
);
只知道用这种方式来绑定静态方法,有方法可以绑定一个实例对象吗?可以在js端直接通过实例对象的名称来调用实例的方法。
谢谢
I have following error after migrate to null safety .
lib/js/js_engine.dart:71:32: Error: Property 'count' cannot be accessed on 'JSValuePointer?' because it is potentially null.
- 'JSValuePointer' is from 'package:myApp/js/js_core_mock.dart' ('lib/js/js_core_mock.dart').
Try accessing using ?. instead.
if (jsContext!.exception.count > 0) {
^^^^^
lib/js/js_engine.dart:71:38: Error: Operator '>' cannot be called on 'int?' because it is potentially null.
if (jsContext!.exception.count > 0) {
^
lib/js/js_engine.dart:72:50: Error: Property 'count' cannot be accessed on 'JSValuePointer?' because it is potentially null.
- 'JSValuePointer' is from 'package:myApp/js/js_core_mock.dart' ('lib/js/js_core_mock.dart').
Try accessing using ?. instead.
for (int i = 0; i < jsContext!.exception.count; i++)
本地js种使用到了require 自己本地通过setProperty的方式 的确让require在执行的时候 回调了回来
但是自己接管了require以后 就等于自身默认不在支持require语法
var jsAlertFunction =
JSObject.makeFunctionWithCallback(_jsContext, 'require', Pointer.fromFunction(require));
_jsContext.globalObject.setProperty('require', jsAlertFunction.toValue(),
JSPropertyAttributes.kJSPropertyAttributeNone);
就会提示报错
我现在的问题 就是 如何能让jscore自身就支持require语法 并且可以自己去找指定文件读取
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.