source upload
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
program HelloSpiderMonkey52;
|
||||
uses
|
||||
SpiderMonkey;
|
||||
begin
|
||||
// TODO: implementation for Delphi
|
||||
end.
|
@@ -0,0 +1,69 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<CONFIG>
|
||||
<ProjectOptions>
|
||||
<Version Value="10"/>
|
||||
<PathDelim Value="\"/>
|
||||
<General>
|
||||
<Flags>
|
||||
<MainUnitHasCreateFormStatements Value="False"/>
|
||||
<MainUnitHasTitleStatement Value="False"/>
|
||||
</Flags>
|
||||
<SessionStorage Value="InProjectDir"/>
|
||||
<MainUnit Value="0"/>
|
||||
<Title Value="HelloSpiderMonkey52"/>
|
||||
<UseAppBundle Value="False"/>
|
||||
<ResourceType Value="res"/>
|
||||
</General>
|
||||
<i18n>
|
||||
<EnableI18N LFM="False"/>
|
||||
</i18n>
|
||||
<VersionInfo>
|
||||
<StringTable ProductVersion=""/>
|
||||
</VersionInfo>
|
||||
<BuildModes Count="1">
|
||||
<Item1 Name="Default" Default="True"/>
|
||||
</BuildModes>
|
||||
<PublishOptions>
|
||||
<Version Value="2"/>
|
||||
</PublishOptions>
|
||||
<RunParams>
|
||||
<local>
|
||||
<FormatVersion Value="1"/>
|
||||
</local>
|
||||
</RunParams>
|
||||
<Units Count="1">
|
||||
<Unit0>
|
||||
<Filename Value="HelloSpiderMonkey52.lpr"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
</Unit0>
|
||||
</Units>
|
||||
</ProjectOptions>
|
||||
<CompilerOptions>
|
||||
<Version Value="11"/>
|
||||
<Target>
|
||||
<Filename Value="HelloSpiderMonkey52"/>
|
||||
</Target>
|
||||
<SearchPaths>
|
||||
<IncludeFiles Value="$(ProjOutDir);../..;../../.."/>
|
||||
<Libraries Value="../../mozjs"/>
|
||||
<OtherUnitFiles Value="../..;../../.."/>
|
||||
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
|
||||
</SearchPaths>
|
||||
<Other>
|
||||
<CustomOptions Value="-dSM52"/>
|
||||
</Other>
|
||||
</CompilerOptions>
|
||||
<Debugging>
|
||||
<Exceptions Count="3">
|
||||
<Item1>
|
||||
<Name Value="EAbort"/>
|
||||
</Item1>
|
||||
<Item2>
|
||||
<Name Value="ECodetoolError"/>
|
||||
</Item2>
|
||||
<Item3>
|
||||
<Name Value="EFOpenError"/>
|
||||
</Item3>
|
||||
</Exceptions>
|
||||
</Debugging>
|
||||
</CONFIG>
|
@@ -0,0 +1,100 @@
|
||||
program HelloSpiderMonkey52;
|
||||
{
|
||||
This example has translated from C++ hello world example taken on
|
||||
https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/How_to_embed_the_JavaScript_engine
|
||||
}
|
||||
{$mode objfpc}{$H+}
|
||||
uses
|
||||
{$IFDEF UNIX}{$IFDEF UseCThreads}
|
||||
cthreads,
|
||||
{$ENDIF}{$ENDIF}
|
||||
Classes,
|
||||
SysUtils,
|
||||
SpiderMonkey;
|
||||
|
||||
const
|
||||
script = '''Hello '' + ''world, it is ''+new Date()';
|
||||
filename = 'noname';
|
||||
|
||||
var
|
||||
global_ops: JSClassOps = (
|
||||
addProperty: nil;
|
||||
delProperty: nil;
|
||||
getProperty: nil;
|
||||
setProperty: nil;
|
||||
enumerate: nil;
|
||||
resolve: nil;
|
||||
mayResolve: nil;
|
||||
finalize: nil;
|
||||
call: nil;
|
||||
hasInstance: nil;
|
||||
construct: nil;
|
||||
trace: nil;//@JS_GlobalObjectTraceHook;
|
||||
);
|
||||
|
||||
// The class of the global object.
|
||||
global_class: JSClass = (
|
||||
name: 'global';
|
||||
flags: JSCLASS_GLOBAL_FLAGS;
|
||||
cOps: @global_ops;
|
||||
);
|
||||
|
||||
cx: PJSContext;
|
||||
options: JS_CompartmentOptions;
|
||||
global: PJSRootedObject;
|
||||
val: jsval;
|
||||
rval: PJSRootedValue;
|
||||
oldCprt: PJSCompartment;
|
||||
lineno: Integer = 1;
|
||||
opts: PJSCompileOptions;
|
||||
ok: Boolean;
|
||||
str: PJSString;
|
||||
|
||||
I: Integer;
|
||||
Frames: PPointer;
|
||||
|
||||
begin
|
||||
try
|
||||
JS_Init();
|
||||
cx := JSContext.CreateNew(8 * 1024 * 1024);
|
||||
try
|
||||
cx^.BeginRequest(); // In practice, you would want to exit this any
|
||||
try // time you're spinning the event loop
|
||||
// Scope for our various stack objects (JSAutoRequest, RootedObject), so they all go
|
||||
// out of scope before we JS_DestroyContext.
|
||||
global := cx^.NewRootedObject(cx^.NewGlobalObject(@global_class));
|
||||
if (global = nil) then
|
||||
Halt(1);
|
||||
rval := cx^.NewRootedValue(val);
|
||||
oldCprt := cx^.EnterCompartment(global^.ptr);
|
||||
try // Scope for JSAutoCompartment
|
||||
cx^.InitStandardClasses(global^.ptr);
|
||||
opts := cx^.NewCompileOptions();
|
||||
opts^.filename := filename;
|
||||
//opts^.?? := lineno;
|
||||
ok := cx^.EvaluateScript(opts, script, Length(script), rval^.ptr);
|
||||
if (not ok) then
|
||||
Halt(1);
|
||||
finally
|
||||
cx^.LeaveCompartment(oldCprt);
|
||||
end;
|
||||
str := rval^.ptr.asJSString;
|
||||
WriteLn(str^.ToAnsi(cx));
|
||||
finally
|
||||
cx^.EndRequest();
|
||||
end;
|
||||
finally
|
||||
cx^.Destroy();
|
||||
JS_ShutDown();
|
||||
end;
|
||||
Halt(0);
|
||||
except
|
||||
on E: Exception do begin
|
||||
Writeln(E.Message);
|
||||
Writeln(BackTraceStrFunc(ExceptAddr));
|
||||
Frames := ExceptFrames;
|
||||
for I := 0 to ExceptFrameCount - 1 do
|
||||
Writeln(BackTraceStrFunc(Frames[I]));
|
||||
end;
|
||||
end;
|
||||
end.
|
Reference in New Issue
Block a user