flutter_quickjs

Flutter bindings with dart:ffi for QuickJS : A small Javascript engine supports ES2020.

This is a plugin help execute javascript on flutter app, which is convenient to use with simple apis, and it supports iOS, Android now.

Install

To use this plugin, add flutter_quickjs as a dependency in your pubspec.yaml file.

Usage

Basic Example

import 'package:flutter/material.dart';
import 'package:flutter_quickjs/flutter_quickjs.dart';

void main() {
  var evalResult = runJs();
  runApp(
    MaterialApp(
      home: Material(
        child: Center(
          child: Text(evalResult),
        ),
      ),
    ),
  );
}

runJs() {
  var qjs = FlutterQuickjs();
  var res = qjs.eval('Math.PI');
  qjs.close();
  return res.toString();
}

Global Object

qjs.eval('var a = 666;');
print(qjs.global()['a']);
// 666

Set Value

qjs.setValue('globalThis.console.log', (msg) {
  print(msg);
});
qjs.eval('console.log("hello world!")');
// hello world!

JS Function Call

var func = qjs.eval('function func(a, b){return [a, b, a + b];}func');
print(func(2,3));
// [2, 3, 5]

For more usages please see example

Datatype Mapping

dartjs
nullUndefined / Null
BoolBoolean
IntNumber
DoubleNumber
StringString
ListArray
MapObject
FunctionFunction
ExceptionError

Todo

  • bytecode support
  • support more platforms like macos,linux

Reference

Lincense

MIT © ZhangPingFan

Libraries

flutter_quickjs