You are not logged in.
Hi Arnaud,
While migrating the mORMot 1 examples (specifically Example 22 "JavaScript HTTPApi web server" and Example 23 "JavaScript Tests") to mORMot 2, I noticed that although mORMot 2 has integrated QuickJS via mormot.lib.quickjs, it lacks a high-level wrapper similar to what SynSM (SpiderMonkey) provided in mORMot 1.
I've implemented TQuickJSEngine in mormot.script.quickjs.pas to fill this gap. The main features include:
TQuickJSEngine - Thread-safe JavaScript engine (inherits from TThreadSafeEngine):
- Evaluate() - Execute JavaScript code and return result as variant
- Global property - Late-binding variant access to the global object (e.g., Engine.Global.myVar := 123)
- RegisterMethod() - Register Delphi methods callable from JavaScript
- TimeoutValue - Script execution timeout control
- GarbageCollect() / MaybeGarbageCollect() - Manual GC control
TQuickJSVariant - Custom variant type for late-binding:
- Allows Pascal code to access JS object properties using variant syntax
- Example: jsObj.propName or jsObj.method(arg1, arg2)
TQuickJSObject - Wrapper for JavaScript object operations:
- HasProperty(), HasOwnProperty()
- GetPropValue(), SetPropValue(), DefineProperty()
- RunMethod() - Call a method on the object
- Root() / UnRoot() - GC protection
This enables straightforward migration of mORMot 1 JavaScript-based examples and provides a convenient API for embedding JavaScript in Delphi/FPC applications.
The implementation leverages the existing mormot.script.core framework and follows mORMot 2 coding conventions.
PR: [https://github.com/synopse/mORMot2/pull/425]
Looking forward to your feedback!
Offline