#1 Today 06:36:10

zen010101
Member
Registered: 2024-06-15
Posts: 159

[PR] Add TQuickJSEngine High-Level Wrapper for QuickJS

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

#2 Today 15:11:09

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,347
Website

Re: [PR] Add TQuickJSEngine High-Level Wrapper for QuickJS

Nice!

I have merged and refactored or fixed some of it.

Thanks for sharing!

Offline

Board footer

Powered by FluxBB