JavaScript Core package

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

Moderator: sysrpl

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.

uses
JavaScript.Core;

var
Context: JSContext;
begin
Context := JSContext.Create;
Context.ScriptRun(SomeJavaScriptString);
end;


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:

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


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

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

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


And in JavaScript you might write:

let s = hello("i am javascript");
console.log(s);


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:

JSEnabledModuleSystem(Context);


And in JavaScript you might write:

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

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


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:

#!/home/user/bin/jsc
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.js

Hello World!

Return to “Webkit Browser and JavaScript Core”

Who is online

Users browsing this forum: No registered users and 1 guest

cron