JavaScript Core package

A discussion forum related to using the Webkit browser and the JavaScript Core.packages for Free Pascal

Moderator: sysrpl

Posts: 108
Joined: Thu Feb 05, 2015 6:31 pm

JavaScript Core package

Postby sysrpl » Fri May 18, 2018 4:12 pm

This post relates to the design and usage of the JavaScript Core (JSC) package. The JSC package provides Free Pascal users with the ability to make use of the Webkit JavaScript Core engine in their native code applications. It can also be used to interface with JavaScript running in any page when paired with the Webkit browser.

I am looking for ideas and help testing with the JSC package. The system JSC I've come up with is very easy to use, and also quite powerful. Before it's actually published I will create a few demos and instructional videos. If you are interested in leaning more about JSC, here is an easy getting starting guide.


Context: JSContext;
Context := JSContext.Create;

Note, JSContext and all related JS types (JSValue, JSObject, JSString) are all managed for you, meaning you do not need to free them. To create a JavaScript object, such as the RegExp object, in Pascal you could use code like:

R: JSObject;
V: JSValue;
Obj := Context.CreateObject('RegExp');
V := Obj.Invoke('exec');
Caption := V.AsString;

To define a callback for JavaScript to invoke your Pascal code you would write:

function PascalHello(Context: JSContext; Args: JSValueArray; var Error: JSValue): JSValue;
if Length(Args) > 0 then
Result := Context.CreateString('Hi there ' + Args[0].AsString)
Result := Context.CreateString('Pascal says hello');

Context.GlobalObject.CreateFunction('hello', PascalHello);

And in JavaScript you might write:

let s = hello("i am javascript");

I also provide a way to define and load entire Pascal modules in JavaScript. A Pascal module is a set of objects/functions that add functionality to JavaScript through native Pascal code. To use a Pascal module you write:


And in JavaScript you might write:

function test() {
system.file.writeText("hello.txt", "hello world");
let s = system.process.execute("ls", "-al");

system.modules.load("file", "process");

Note, the module system provides a default console object even if you are operating outside a web browser. Also, the module system is available even if you are executing the JavaScript inside a web page. I will provide a reference on how to write your own modules.

Finally, included with the JSC package is a demo application named jsc. The jsc application is a scripting program that allows you to run scripts on Mac/Linux via shebangs:

console.log("Hello World!");

You can load modules in jsc scripts as well. To run your script, assuming it is called hello.js, from a terminal:

chmod +x hello.js

Hello World!

Return to “Webkit Browser and JavaScript Core”

Who is online

Users browsing this forum: No registered users and 1 guest