# flujs **Repository Path**: chdown/flujs ## Basic Information - **Project Name**: flujs - **Description**: flujs: js engine binding for flutter - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2025-01-08 - **Last Updated**: 2025-01-08 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # flujs execute js code in flutter, supported by JavaScriptCore or QuickJS. | status | android | iOS | windows | linux | darwin | | ------- | -------------- | -------------- | -------------- | -------------- | -------------- | | default | QuickJS | JavaScriptCore | QuickJS | QuickJS | JavaScriptCore | | forceJSC | JavaScriptCore | JavaScriptCore | JavaScriptCore | JavaScriptCore | JavaScriptCore | | forceQJS | QuickJS | QuickJS | QuickJS | QuickJS | QuickJS | ## features - easily to use: js call native or native call js. - support xhr、promis、fetch、setTimeout、setInterval、console intrinsic functions. - xhr depends on [http](https://pub.dev/packages/http). so it can be easily extends with other ability(replace client with dio, support httpCache、http2 etch). - friendly dart api. - support multiple JSRuntime concurrently running. ## Getting Started 1、get JSFRuntime ```dart var rt = getJSRuntime(); // depends on platform // var rt = getJSRuntime(forceQJS: true); // force use QuickJS ``` 2、get JSFContext ```dart var ctx = rt.newContext(); ``` 3、load intrinsic functions. ```dart ctx.loadExtension(); ``` 4、execute js code ```dart var res = ctx.eval('''eval('2+2')'''); ``` 5、js call native ```dart // common function ctx.addInterface('run', (args) { debugPrint('[native] $args'); return 1; }); // async function, need return a future. ctx.addInterface('runAsync1', (args) async { var completer = Completer(); Timer(const Duration(seconds: 1), () { completer.complete(1); }); return completer.future; }); ctx.addInterface('runAsync2', (args) async { var r = await asyn2(1); return r; }); Future asyn2(int delaySecs) async { var completer = Completer(); Timer(const Duration(seconds: delaySecs), () { completer.complete(1); }); return completer.future; } var res = ctx.eval(''' run('from js', 2); runAsync1().then(() => runAsync2()).then(console.log); 3; '''); debugPrint('runAsync: $res'); ``` 6、native call js ```js function injs(time) { console.log('injs -> ', time); } ``` ```dart var injs = ctx.globalObject.getProperty('injs'); assert(injs.isFunction()); injs.callAsFunction(arguments: [ JSFValueFactory.makeNumber(ctx, id) ]); ``` ## API [xhr](doc/api/xhr.md) ## Work In Progress #### Memory leak - [✅] QJS Runtime/Context manual free will cause crash. - [✅] memory leak, memory detection - [✅] Dart Object release native reference when garbage collected with NativeFinalizer - [✅] JSValue.object(create in stack) released when function call end. - [✅] Dart Value Finalizer and release native pointer. #### Cross platform build / flutter integration - [x] QuickJS cross compile support: cmake、sh、UPX、xmake. native_assets or cmake #### Test - [x] [tc39 test262](https://github.com/tc39/test262) #### Function ability - [✅] addInterface auto convert jsc.JSValue/JSFValue(qjs) to dart type(dynamic). user do not need to check platform's type before using. - [✅] concurrence: multiple JSVirtualMachine, multiple JSContext - [✅] export JSFExtension's core property, user could do some custom work. like add HttpClient Cache with xhr or fetch extension. - [✅] Dart/JavaScript Object <------> String transfer - [x] worker or dart isolate support. - [✅] Timer move to new isolate to execute. - [x] QuickJS use Module or global function/variable to expose intrinsic ability! - [x] fetch reponse.headers/blob/clone .... - [x] JSON.stringify/JSON.parse replace in dart side. #### Bug - [x] toDart isArray cause crash. - [x] QJS xhr addInterface decode HttpHeader failed. - [x] bilibili.hitPlaylist quickjs cannot pass test. and throw dart/js exception to notify developer. - [x] JSValueQJSImpl toString only print [Object object]. - [x] JSFContext.eval jscode encounter error, cannot catch error in dart side. - [x] QJS JSContext.create(rt,intrinsic: true) will cause crash. ### Thanks - [flutter-js](https://github.com/abner/flutter_js) - [flutter-jscore](https://github.com/xuelongqy/flutter_jscore) - [flutter-qjs](https://github.com/ekibun/flutter_qjs) - [quickjs-android](https://github.com/seven332/quickjs-android) - [jsx](https://github.com/RxReader/jsc) - [minnet-quickjs](https://github.com/khanhas/minnet-quickjs)