A dart binding for QuickJS, a modern Javascript interpreter written in C by Fabrice Bellard
We can run javascript VM embedded to DartVM, using Dart:FFI
- dart lang 2.8+
- clang
- llvm(optional)
- ios 9+
- android api 21+
// dart code
import 'package:quickjs_dart/quickjs_dart.dart';
void main(){
JSEngine(); // initialize engine
String jsString = r"""
function testAdd(x,y){
return x+y;
}
testAdd
"""
var engine= JSEngine.instance; // singleton
var testAdd = engine.evalScript(jsString);
print(testAdd.isFunction()); // true
var result= testAdd.callJS([engine.newInt32(12),engine.newInt32(34)]); // 2 params, 12 and 34;
result.jsPrint(); // use `console.log` in javascript, 46 is the result;
}
-
build quickjs lib for ios/android/dartVM
sh ~/.build_all.sh
or build android only
sh ~/.build_android.sh
or bulid ios only
sh ~/.build_ios.sh
-
run dart on dart vm
dart main.dart
-
if you come up with
file system relative paths not allowed in hardened programs
with macos, run thiscodesign --remove-signature /usr/local/bin/dart
-
run flutter example, android or ios
note: run
flutter doctor -v
to examine the flutter env is correctlyThen you can run example app
cd example && flutter run
V8 is too big for small app and IOT devices. jscore is a bit old and slow for modern app.
Quickjs follows latest Javascript standard (ES2020) now. And it is fast enough, see benchmark
PlatformChannel/MethodChannel(s) are designed for communication, post and receive data, and use features that had been made by exisiting Android/iOS/Native modules. It's not managed by Dart/Flutter itself.
Using Dart:FFI, we get possibilities to expand the dart/flutter. We can call native function, back and forward, adding Callbacks, manage memory of functions and values.
Do it later