Table of Contents
Intro
Many Delphi developers adore the language integration options of the Embarcadero Delphi and C++ Builder. For instance, you can utilize Assembly language within the Delphi code by just opening asm scope. This is an inline assembly feature. Moreover, you can integrate other languages like Java & JavaScript.
With this option, you can create your application 5x faster or even 10x faster with Delphi and C++ Builder. Since this is all about Rapid Application Development (RAD), Delphi & C++ Builder communities love it.
Let’s investigate the JSEngine by WINSOFT which offers to use the Microsoft ChakraCore JavaScript engine with Delphi and C++ Builder. This is a commercial library that has a trial version with all features for evaluation only and if you wish to distribute the JSEngine library as a part of your project you should get a license.
Furthermore, you should know about the Microsoft ChakraCore JS Engine third party notices. Because ChakraCore uses third party projects from different authors.
Notice – Future of ChakraCore
As you may have heard Microsoft Edge no longer uses Chakra. Microsoft will continue to provide security updates for Chakracore 1.11 until 9th March 2021 but do not intend to support it after that. However, ChakraCore is planned to continue as a community project targeted primarily at embedded use cases. A new version plan – Chakracore 1.12
Specifications
- Supports Windows 32 & Windows 64
- Available for Delphi & C++ Builder 6-10.4 VCL
Configuration
The first step is always installing the new libraries into your development environment. Here is a short tutorial on installing the library correctly.
The second step is opening the demo source code and adding the ChakraCore.dll application extension to your project folder. If you are building your application for Windows 32-bit you must copy the x86 ChakraCore.dll extension. Or it is for Windows 64-bit you need to copy the x64 ChakraCore.dll application extension. You can get the application extension from the Library folder.
Development
The third main step is to add the units to the uses block. The result should be like this:
1 |
uses ChakraCommon, JSEngine; |
So, now we are ready to use the JSEngine in our project! Here is the user interface:
To use ChakraCore and JSEngine we should create three instances for TJsEngine (jsengine.pas) and JsValueRef (ChakraCommon.pas)
1 2 3 4 |
var Engine: TJsEngine; MyObject: JsValueRef; Result: JsValueRef; |
All right, now let’s create Engine object and execute simple expression.
1 |
Engine := TJsEngine.Create; |
Now, we should get the JavaScript code from the MemoScriptSimple component and call RunScript function, after that we can convert the result to String with Engine.ConvertToString function. Here is the implementation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
var Engine: TJsEngine; MyObject: JsValueRef; Result: JsValueRef; begin Engine := TJsEngine.Create; with Engine do try MemoResultSimple.Text := ConvertToString(RunScript(MemoScriptSimple.Text)); except on E: Exception do ShowException(MemoResultSimple, E, Engine); end; end; end. |
If you would like to use a native function, you should register it first with an exact name of the function. Then you can run your native function. Here is the implementation:
1 2 |
RegisterFunction('NativeFunction', NativeFunction, Engine); MemoResultNativeFunction.Text := ConvertToString(RunScript(MemoScriptNativeFunction.Text)); |
If you would like to run your functions it takes little more work, because you need to first run the script itself and you should call the function with needed parameters.
For instance, you want to run this script:
1 2 3 |
function hello(name) { return 'Hello, ' + name + '!'; } |
For this, you should implement your function like this:
1 2 3 4 5 6 7 8 9 10 11 |
Engine := TJsEngine.Create; with Engine do try RunScript(MemoScriptFunctionCall.Text); Result := Call('hello', [UndefinedValue {this}, CreateString('Tech Partner')]); MemoResultFunctionCall.Text := ConvertToString(Result); except on E: Exception do ShowException(MemoResultFunctionCall, E, Engine); end; end; |
Nice! It all goes like this. Check out the full WINSOFT JSEngine for Delphi and C++ Builder!
Like what you see? You can get the JSEngine Component and over 100 other fantastic WinSoft components with our Enterprise Component Pack. For a limited time, when you purchase RAD Studio Enterprise or Architect Edition at special Upgrade Pricing, you will also get this package of third-party software worth over $13,000, including the full WinSoft Component Library, at NO EXTRA COST! Step up to RAD Studio 10.4 today!
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition