#1 2016-12-25 20:14:13

chronisvel
Member
Registered: 2016-01-01
Posts: 8

uniGui - Server Side JavaScript with SpiderMonkey

Hi, I tried implementing server side javascript with uniGui, using SpiderMonkey, SyNode. I always get an error :
Project XXXXX.exe raised exception class $C0000005 with message 'c00000005 ACCESS_VIOLATION'

When running the same lines of code in a VCL application everynting works fine.

The exception Is raised in Spidermonkey Unit in the line :
function JS_NewRuntime(maxbytes: uint32; maxNurseryBytes: uint32; parentRuntime: PJSRuntime): PJSRuntime;
  cdecl; external SpiderMonkeyLib;

What should I do? Is mormot's implementation incompatible with uniGui? If so what are my alternatives?

Thanks...

Offline

#2 2016-12-25 21:31:13

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,240
Website

Re: uniGui - Server Side JavaScript with SpiderMonkey

Which compiler?

On which line of code does the exception be raised?

Is it linked to http://synopse.info/forum/viewtopic.php?id=1733 ?

There is no enough information here to investigate.
Please put some more details (logs, stack traces) on separated https://gist.github.com/ not in the forum.
We need code to reproduce the issue (e.g. an archive in dropbox).

Offline

#3 2016-12-26 11:49:01

chronisvel
Member
Registered: 2016-01-01
Posts: 8

Re: uniGui - Server Side JavaScript with SpiderMonkey

Hi, I am using Embarcadero Delphi 10.1 Berlin and uniGui 0.99.96 build 1322.
Your demos run perfectly. New VCL applications using Spidermonkey + required dlls run perfectly.
The problem is when I create a uniGui application then I have this problem. For example I have the following code :


unit ServerModule;

interface

uses
  Classes, SysUtils, uniGUIServer, uniGUIMainModule, uniGUIApplication, uIdCustomHTTPServer,
  uniGUITypes,shellapi,  SynCommons,
  SpiderMonkey,
  SyNode,
  SyNodeProto,
  SyNodeSimpleProto;

type
  TUniServerModule = class(TUniGUIServerModule)
    procedure UniGUIServerModuleCreate(Sender: TObject);
  private
    { Private declarations }
  protected
    FSMManager: TSMEngineManager;
    FEngine: TSMEngine;
    procedure DoOnCreateNewEngine(const aEngine: TSMEngine);
    function DoOnGetEngineName(const aEngine: TSMEngine): RawUTF8;
    procedure doInteruptInOwnThread;
    procedure DoOnJSDebuggerInit(const aEngine: TSMEngine);
    procedure FirstInit; override;
  public
    { Public declarations }
  end;

function UniServerModule: TUniServerModule;

implementation

{$R *.dfm}

uses
  UniGUIVars;

function UniServerModule: TUniServerModule;
begin
  Result:=TUniServerModule(UniGUIServerInstance);
end;

procedure TUniServerModule.doInteruptInOwnThread;
begin

end;

procedure TUniServerModule.DoOnCreateNewEngine(const aEngine: TSMEngine);
begin
  aEngine.doInteruptInOwnThread := doInteruptInOwnThread;
  // in XE actual class of TMemo.lines is TMemoStrings - let's force it to be a TStrings like
  //aEngine.defineClass( TStrings, TStringsProto, aEngine.GlobalObject);
  // define a propery mainForm in the JavaScript
  aEngine.GlobalObject.ptr.DefineProperty(aEngine.cx, Pansichar( self.ClassName) ,
    // proeprty value is a wrapper around the Self
    CreateJSInstanceObjForSimpleRTTI(aEngine.cx, Self, aEngine.GlobalObject),
    // we can enumerate this property, it read-only and can not be deleted
    JSPROP_ENUMERATE or JSPROP_READONLY or JSPROP_PERMANENT
  );

end;

function TUniServerModule.DoOnGetEngineName(const aEngine: TSMEngine): RawUTF8;
begin
  Result:='My JSEngine';
end;

procedure TUniServerModule.DoOnJSDebuggerInit(const aEngine: TSMEngine);
begin

end;

procedure TUniServerModule.FirstInit;
begin
  InitServerModule(Self);
end;

procedure TUniServerModule.UniGUIServerModuleCreate(Sender: TObject);
var res: jsval;
begin
  FSMManager := TSMEngineManager.Create(StringToUTF8(ExtractFilePath( ParamStr(0) )+'\core_modules'));
  // optionaly increase a max engine memory
  FSMManager.MaxPerEngineMemory := 512 * 1024 * 1024;
  // add a handler called every time new engine is created
  // inside a handler we can add a binding's to a native functions (implemented in Delphi)
  // and evaluate some initial JavaScripts
  FSMManager.OnNewEngine := DoOnCreateNewEngine;
  FSMManager.OnGetName := DoOnGetEngineName;
  //FSMManager.OnDebuggerInit := DoOnJSDebuggerInit;
  // start a JavaScript debugger on the localhost:6000
  //FSMManager.startDebugger('6000');
  // debugger can see engines created after startDebugger() call,
  // so we create a main engine after debugger is started
  // in this example we need only one engine (we are single-thread)
  //Exception is raised here
  FEngine := FSMManager.ThreadSafeEngine(nil);
  FEngine.Evaluate('var i=0;',Self.ClassName+'.js', 1, res)

end;

initialization
  RegisterServerModuleClass(TUniServerModule);
end.

Hope this helps!

Offline

#4 2016-12-26 11:59:56

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,544
Website

Re: uniGui - Server Side JavaScript with SpiderMonkey

We don't know what is uniGui, but looks like you miss InitJS; / ShutDownJS; calls (in demo it is inside SpiderMonkey45Binding.dpr)
InitJS / ShutDownJS must be call ONCE (not per thread)

Last edited by mpv (2016-12-26 12:00:47)

Offline

#5 2016-12-26 12:14:23

chronisvel
Member
Registered: 2016-01-01
Posts: 8

Re: uniGui - Server Side JavaScript with SpiderMonkey

Thank you very much! You were correct, excellent support...

Offline

Board footer

Powered by FluxBB