Language: English | 简体中文
dart_quickjs
is a binding for the QuickJS
engine using Dart, which supports executing JavaScript code on Android
and iOS
. QuickJS is a lightweight JavaScript engine that supports the ECMAScript 2020 specification.
$ dart pub add dart_quickjs
If you need to specify a version, you can add it to the pubspec.yaml
file.
dependencies:
dart_quickjs: ^Version number
println
is the print function injected by dart_quickjs
.
import 'package:dart_quickjs/dart_quickjs.dart';
final runtime = Runtime();
const script = 'println("Hello World");';
runtime.evaluateJavaScript(script, 'main.js');
Dart | JavasCript |
---|---|
JSObject | Object |
JSString | String |
JSNumber | Number |
JSFloat | Number |
JSBool | Boolean |
JSArray | Array |
JSFunction | Function |
JSPromise | Promise |
JSNull | null |
JSUndefined | undefined |
import 'package:dart_quickjs/dart_quickjs.dart';
final runtime = Runtime();
// Creating and injecting objects
final value = JSString.create(runtime.context, 'dart_quickjs');
// Add to global objects
runtime.global.setPropertyStr('name', value);
// Print name
runtime.evaluateJavaScript('println(name);', 'main.js');
import 'package:dart_quickjs/dart_quickjs.dart';
final runtime = Runtime();
// Creating methods
final value = JSFunction.create(runtime.context, (JSNumber data) {
return JSNumber.create(runtime.context, data.value + 1);
});
// Add to global objects
runtime.global.setPropertyStr('add', value);
// Execute code
runtime.evaluateJavaScript('println(add(1));', 'main.js');
Calling Dart
from JavaScript
import 'package:dart_quickjs/dart_quickjs.dart';
final runtime = Runtime();
// Creating methods
final value = JSFunction.create(runtime.context, (JSString message) {
print('JavaScriptMessage: ${message.value}');
});
// Add to global objects
runtime.global.setPropertyStr('test', value);
// Execute code
runtime.evaluateJavaScript('test("dart_quickjs");', 'main.js');
Calling JavaScript
from Dart
import 'package:dart_quickjs/dart_quickjs.dart';
final runtime = Runtime();
// JavaScript code
const code = '''function message(value) {
return value + 1;
}
message;''';
// Execute code
final callback = runtime.evaluateJavaScript(code, 'main.js') as JSFunction;
// Calling JavaScript methods
final value = callback.call([JSNumber.create(runtime.context, 1)]);
// Print return value
print(value);
In communication, you can pass basic types and function types.
- When using the module system, you need to pass the
moduleLoader
method to load modules. - When running the entry point, the run mode needs to be set to
JSEvalType.module
.
import 'package:dart_quickjs/dart_quickjs.dart';
final runtime = Runtime(
moduleLoader: (String name) {
if (name == 'message') {
return "export const message = 'dart_quickjs'";
}
return '';
},
);
// main.js
const main = '''
import { message } from 'message'
println(message)
''';
// Execute code
runtime.evaluateJavaScript(main, 'main.js', JSEvalType.module);
import 'package:dart_quickjs/dart_quickjs.dart';
class Test extends Plugin {
late Runtime _runtime;
@override
void onCreate(Runtime runtime) {
_runtime = runtime;
// Injection Method
runtime.global.setPropertyStr(
'getMessage',
JSFunction.create(runtime.context, test),
);
}
JSString test(JSString data) {
return JSString.create(_runtime.context, '${data.value} World');
}
@override
void destroy(Runtime runtime) {}
}
final runtime = Runtime(
plugins: [
// Register Plugin
Test()
],
);
const script = 'println(getMessage("Hello"));';
runtime.evaluateJavaScript(script, 'main.js');
import 'package:dart_quickjs/dart_quickjs.dart';
final runtime = Runtime();
const script = 'println("Hello World");';
// Compile Bytecode
final bytecode = runtime.compile(script, 'main.js');
// Run Bytecode
runtime.evaluateBytecode(bytecode);
import 'package:dart_quickjs/dart_quickjs.dart';
final runtime = Runtime();
runtime.destroy();
println
setInterval
clearInterval
setTimeout
clearTimeout
This document was translated using ChatGPT. 🎉