सार्वजनिक जानकारी से बना चयन मार्गदर्शन। केवल संदर्भ हेतु।
jsf एक उच्च प्रदर्शन वाला JavaScript इंजन है जो सीधे Flutter में एकीकृत है, जिससे कई प्लेटफॉर्मों पर JavaScript कोड के बिना कठिनाइयों के निष्पादन संभव होता है। दक्षता और उपयोग में आसानी के लिए डिज़ाइन किया गया, यह विकासकर्ताओं को बाहरी निर्भरता के बिना Flutter ऐप्स में JavaScript चलाने की अनुमति देता है। इंजन पूर्ण JavaScript अंतरक्रिया का समर्थन करता है, जिससे यह स्क्रिप्ट एम्बेड करने, डायनामिक लॉजिक निष्पादित करने या मौजूदा JavaScript लाइब्रेरी के साथ एकीकरण के लिए आदर्श है। यह वास्तविक समय में स्क्रिप्ट मूल्यांकन या क्रॉस-प्लेटफॉर्म संगतता की आवश्यकता वाले मामलों में विशेष रूप से उपयोगी है। Android, iOS, Web, macOS, Windows और Linux के समर्थन के साथ, jsf वातावरणों के बीच स्थिर व्यवहार सुनिश्चित करता है।
उपयोग
Flutter ऐप्स में डायनामिक JavaScript कोड चलाना
पुराने जावास्क्रिप्ट पुस्तकालयों का एकीकरण
स्क्रिप्टेबल UI घटक सक्षम करें
रियल-टाइम कोड मूल्यांकन
क्रॉस-प्लेटफॉर्म जावास्क्रिप्ट निष्पादन
मुख्य विशेषताएँ
उच्च प्रदर्शन जावास्क्रिप्ट इंजन
मूल फ्लटर एकीकरण
बहु-प्लेटफॉर्म समर्थन (Android, iOS, Web, macOS, Windows, Linux)
अनुप्रयोग जावास्क्रिप्ट इंटरॉप
हल्का और तेज़ स्टार्टअप
इनके लिए उपयुक्त
Flutter में JavaScript रनटाइम की आवश्यकता वाले विकासकर्ता
गतिशील स्क्रिप्ट की आवश्यकता वाले ऐप्स
मौजूदा JS पुस्तकालयों का उपयोग करने वाले प्रोजेक्ट
क्रॉस-प्लेटफॉर्म एप्लिकेशन
प्रदर्शन-संवेदनशील वातावरण
विचार योग्य बातें
JavaScript सुरक्षा जोखिमों के सावधानीपूर्वक प्रबंधन की आवश्यकता होती है
एप्लिकेशन के आकार में हल्का वृद्धि हो सकती है
मूल जेएस परिवेश की तुलना में सीमित डीबगिंग उपकरण
भारी सर्वर-साइड जावास्क्रिप्ट लोड के लिए उपयुक्त नहीं
मूल README
संबंधित सोर्स रिपॉज़िटरी के मूल README का स्नैपशॉट। नवीनतम सामग्री के लिए रिपॉज़िटरी देखें।
Owned JsValue handles must be disposed. Handles received inside registerHandleFunction are borrowed and only valid for the callback duration. Call duplicate() if you need to keep one.
Dart Calls JavaScript
final add = js.evalValue('(function(a, b) { return a + b; })');
try {
final result = js.callValue(add, [20, 22]);
try {
print(result.toDart()); // 42
} finally {
result.dispose();
}
} finally {
add.dispose();
}
Use registerFunction() to expose a Dart function on the JavaScript global
object. JavaScript calls it like a normal function. Arguments are converted to
Dart values, and the return value is converted back to JavaScript:
js.registerFunction('dartSum', (args) {
return args.cast<num>().reduce((a, b) => a + b);
});
print(js.eval('dartSum(4, 5, 6)')); // 15
Callbacks can receive multiple arguments and return convertible values such as
Map, List, BigInt, and DateTime:
js.registerFunction('receiveMessage', (args) {
final name = args[0] asString;
final payload = args[1] asMap;
return {
'ok': true,
'message': '$name:${payload['count']}',
};
});
print(js.eval('receiveMessage("counter", {count: 3}).message')); // counter:3
Callbacks may return Future; JavaScript receives a Promise, so JS can use
await or .then() directly:
js.registerFunction('loadUser', (args) async {
return {'id': 1, 'name': 'Ada'};
});
final user = await js.evalAsync('loadUser().then((user) => user.name)');
With JavaScript async/await:
final name = await js.evalAsync('''
(async () => {
const user = await loadUser();
return user.name;
})()
''');
print(name); // Ada
registerFunction() converts JavaScript objects into Dart snapshots. Use
registerHandleFunction() when you need to preserve JavaScript object identity
or work with functions, class instances, circular objects, or host objects. It
passes arguments to Dart as JsValue handles:
js.registerHandleFunction('readModel', (args) {
final model = args.first;
final count = model.getPropertyValue('count');
try {
return count.toDart();
} finally {
count.dispose();
}
});
Arguments received by registerHandleFunction() are borrowed handles and are
only valid for the callback duration. Call duplicate() if you need to keep one
outside the callback, and dispose the owned handle when finished.
Promise
When JavaScript returns a Promise, evalAsync() gives you the resolved Dart
value:
final value = await js.evalAsync('Promise.resolve({ok: true})');
print(value); // {'ok': true}
evalAsync() is also the normal way to call async function:
final result = await js.evalAsync('''
async function compute() {
const value = await Promise.resolve(21);
return value * 2;
}
compute()
''');
print(result); // 42
Native platforms use the QuickJS module loader. Web includes JSF's registry
loader behind the same Dart API and supports in-memory modules, import maps,
relative resolution, module caching, static import, named/default/namespace
exports, re-exports, and literal dynamic import("module").
evalAsync(..., module: true) and dynamic import() on Web use the browser's
native ES Module/Blob loader, so browser-supported ESM syntax works, including
export * from, export { x } from, export * as ns from, and top-level
await. The in-memory module loader is intended for application scripts and
Flutter asset modules; it does not fetch network module URLs.
A JsRuntime owns one QuickJS runtime and one context.
Use a runtime from the same Dart isolate that created it.
Dispose every runtime with dispose().
Dispose owned JsValue handles when you are done.
Do not use handles after their runtime is disposed.
Disposing a runtime also releases owned JsValue handles still registered under that runtime, but explicit dispose() remains recommended to control memory peaks.
Testing
Integration tests live in example/integration_test:
cd example
flutter drive --driver=integration_test/driver.dart --target=integration_test/js_runtime_test.dart -d macos
The test suite covers primitive conversion, BigInt, object/array conversion, handle calls, Dart callbacks, promise waiting, module loading, exceptions, timeouts, Unicode, typed arrays, circular objects, and multiple runtimes.