fjs library
FJS - Flutter JavaScript Engine
A comprehensive Flutter library for executing JavaScript code within Flutter applications. Built on top of the QuickJS engine, FJS provides a seamless integration between Dart and JavaScript, enabling developers to run JavaScript code, use Node.js modules, and create hybrid applications.
Features
- Synchronous and Asynchronous Execution: Support for both sync and async JavaScript operations
- Module System: Full ES6 module support with dynamic loading capabilities
- Node.js Compatibility: Built-in support for common Node.js modules
- Bidirectional Communication: Call JavaScript from Dart and Dart from JavaScript
- Memory Management: Fine-grained control over memory usage and garbage collection
- Type Safety: Type-safe conversion between Dart and JavaScript values
- Error Handling: Comprehensive error reporting and debugging capabilities
Basic Usage
import 'package:fjs/fjs.dart';
// Create an async runtime
final runtime = await JsAsyncRuntime.withOptions(
builtin: JsBuiltinOptions.all(),
);
// Create a context
final context = await JsAsyncContext.from(runtime);
// Create an engine
final engine = JsEngine(context: context);
// Initialize the engine
await engine.initWithoutBridge();
// Execute JavaScript code
final result = await engine.eval(
source: JsCode.code('Math.random() * 100'),
);
print('Random number: ${result.value}');
Module Usage
// Load a module from file
await engine.declareNewModule(
module: JsModule.path('utils', '/path/to/utils.js'),
);
// Execute a function from a module
final result = await engine.eval(
source: JsCode.code("import { add } from 'utils'; add(2, 3);"),
);
Bridge Communication
await engine.init(
bridge: (value) async {
print('JavaScript called: ${value.value}');
return JsResult.ok(JsValue.string('Hello from Dart!'));
},
);
Classes
- JsAsyncContext
- JsAsyncRuntime
- JsBuiltinOptions
- Options for configuring builtin Node.js modules.
- JsCode
- JsContext
- JsEngine
- JsError
- JsEvalOptions
- Options for JavaScript code evaluation.
- JsModule
- Represents a JavaScript module.
- JsResult
- JsRuntime
- JsValue
- LibFjs
- Main entrypoint of the Rust API
- MemoryUsage
Extensions
- JsBuiltinOptionsPatterns on JsBuiltinOptions
- Adds pattern-matching-related methods to JsBuiltinOptions.
- JsCodePatterns on JsCode
- Adds pattern-matching-related methods to JsCode.
- JsErrorPatterns on JsError
- Adds pattern-matching-related methods to JsError.
- JsEvalOptionsPatterns on JsEvalOptions
- Adds pattern-matching-related methods to JsEvalOptions.
- JsModulePatterns on JsModule
- Adds pattern-matching-related methods to JsModule.
- JsResultPatterns on JsResult
- Adds pattern-matching-related methods to JsResult.
- JsValuePatterns on JsValue
- Adds pattern-matching-related methods to JsValue.