Skip to content

Latest commit

 

History

History
87 lines (73 loc) · 2.26 KB

README.md

File metadata and controls

87 lines (73 loc) · 2.26 KB

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.

CocoaPods: >= 1.10.0

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

dart js
null Undefined / Null
Bool Boolean
Int Number
Double Number
String String
List Array
Map Object
Function Function
Exception Error

Todo

  • bytecode support
  • support more platforms like macos,linux

Reference

Lincense

MIT © ZhangPingFan