#1 2020-08-24 13:21:14

gd
Member
Registered: 2018-07-19
Posts: 3

Problems with TSMEngineManager inside a DLL module

Hello

I'm trying to use TSMEngineManager as an engine to run a JavaScript in Delphi application.

It works fine but I want to put it inside a closed DLL module and run it with a JavaScript as an method argument. But I have encountered a problem. When I create an instance of TSMEngineManager inside a DLL method and free it, then my application crashes after about 5 seconds after the FreeLibrary().
I have reduced my test application to the absolute minimum and it still crashes everytime. Now it doesn't do anything beside creating and freeing the TSMEngineManager. When I remove the creation of TSMEngineManager, it doesn't crash.

Am I doing something wrong?

Delphi version used: XE 10.1 Berlin
Tested on the newest mORMot from here https://github.com/synopse/mORMot (master).
DLL files needed to run the project: libnspr4.dll and mozjs-24.dll (I have both from 2017 - are they the most recent ones?).
Maybe I use wrong version of mozjs-24.dll? I know it's pretty old SM implementation but I'm not sure how to use SM45 or SM52 and where to find new DLLs for it.

Here's the example. It has 2 DPR files only:

---------------------------------------------------------------
DLL file
---------------------------------------------------------------

library JavaScriptRunnerLib;

uses
  SynSM,
  SynCommons;

{$R *.res}

procedure RunMethodTest(); stdcall;
var
  SMEngineManager: TSMEngineManager;
begin
  SMEngineManager := TSMEngineManager.Create;
//  SMEngineManager.ReleaseCurrentThreadEngine; // < it doesn't help at all
  SMEngineManager.Free;
end;

exports
  RunMethodTest;

begin

end.

---------------------------------------------------------------
Host application
---------------------------------------------------------------

program JavaScriptRunnerExample;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  Winapi.Windows,
  System.SysUtils;

type
  TRunMethodTestProc = procedure (); stdcall;

const
  ScriptRunnerLibFileName = 'JavaScriptRunnerLib.dll';

function LoadScriptRunnerLibrary: THandle;
begin
  result := LoadLibrary(ScriptRunnerLibFileName);
//  result := SafeLoadLibrary(ScriptRunnerLibFileName, SEM_FAILCRITICALERRORS);  // < it doesn't help
  if result = 0 then
    raise exception.CreateFmt('Error loading library %s', [ScriptRunnerLibFileName]);
end;

procedure Test2;
var
  LibHandle: THandle;
  RunMethodTestProc: TRunMethodTestProc;
begin
  LibHandle := LoadScriptRunnerLibrary;
  try
    RunMethodTestProc := GetProcAddress(LibHandle, 'RunMethodTest');
    RunMethodTestProc();
  finally
    FreeLibrary(LibHandle); // < after this command the application will crash within about 5 seconds
      // If I don't use the FreeLibrary() it won't crash but I need to it
  end;
end;

begin
  ReportMemoryLeaksOnShutdown := true;
  try
    writeln('start');
    Test2;
    writeln('sleep');
    sleep(5000);
    writeln('after sleep - press enter');
    //probably it will crash before this line
    Readln;
    writeln('ok');
  except
    on E: exception do
    begin
      writeln(E.ClassName, ': ', E.Message);
      readln;
    end;
  end;

end.

Thank you for any help.

Offline

#2 2020-08-24 17:59:48

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

Re: Problems with TSMEngineManager inside a DLL module

Wokring SyNode version is currently in feature/synodeCleanup brunch. It's for SpiderMonkey52. Links to compiled DLLs is in the SyNode/README.md.
But after Mozilla rewrote most of the part of SpiderMonkey using C++ we lost Deplhi compatibility - please, see this discussion https://synopse.info/forum/viewtopic.php?id=5442.
We use a FPC 3.2.0 (x64) for both Windows and Linux.

In case you do not need a NodeJS build-in modules and JavaScript Debugger the good choice is a Chakra Core wrapper. Chakra API is MUCH simple compared to SM and exported as C.
Or pure Delphi besen ECMAScript (no JIT optimization)

Offline

#3 2021-01-28 16:43:43

MattD
Member
Registered: 2021-01-08
Posts: 1

Re: Problems with TSMEngineManager inside a DLL module

Any plans to regain Delphi compatibility? Is there any reason I shouldn't feel comfortable building my application around an older version that is compatible with Delphi?

Offline

#4 2021-01-29 12:53:36

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

Re: Problems with TSMEngineManager inside a DLL module

A solution for how to import a variable exported by DLL - see this discussion in Delphi should be found to return a Delphi compatibility.

We uses an older version that is compatible with Delphi in production before, so yes, it can be used. But it lack some new features we add to latest version, mostly a nodeJS compatibility (at last hashes in crypto)

Offline

#5 2021-07-24 19:26:12

George
Member
Registered: 2016-04-05
Posts: 142

Re: Problems with TSMEngineManager inside a DLL module

It seems, links are broken sad

Offline

#6 2021-07-25 07:50:26

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

Re: Problems with TSMEngineManager inside a DLL module

@George - I check - all links are valid

Offline

#7 2021-07-25 07:56:43

George
Member
Registered: 2016-04-05
Posts: 142

Re: Problems with TSMEngineManager inside a DLL module

Unable to connect.
Firefox can’t establish a connection to the server at unitybase.info.
I checked also from tor browser, just in case (with IP address from France).

Last edited by George (2021-07-25 08:05:25)

Offline

#8 2021-07-25 14:26:46

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

Re: Problems with TSMEngineManager inside a DLL module

I am from France, and I can access the link with no problem.

I can't ping unitybase.info (I guess this is on purpose) but it is resolved as 91.214.182.35

You may have a DNS issue.

Offline

#9 2021-07-25 14:46:06

George
Member
Registered: 2016-04-05
Posts: 142

Re: Problems with TSMEngineManager inside a DLL module

Ping unitybase.info reports same ip 91.214.182.35
I believe this is some sort of cross country restrictions.
But i have no idea how it affects tor browser as well.

I tested from 4 separate ISPs and 3 different devices from different networks (and from mobile phone as well).

Offline

#10 2021-07-25 19:01:59

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

Re: Problems with TSMEngineManager inside a DLL module

If this is cros country restriction, I think this is is not from my side. unitybase.info is hosted on our private cloud and we do not add any restriction there.
Please, try a google drive - https://drive.google.com/drive/folders/ … sp=sharing

Last edited by mpv (2021-07-25 19:02:09)

Offline

#11 2021-07-25 20:48:33

George
Member
Registered: 2016-04-05
Posts: 142

Re: Problems with TSMEngineManager inside a DLL module

Thank you mpv!

Offline

#12 2021-07-25 20:58:09

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

Re: Problems with TSMEngineManager inside a DLL module

Weird, I have a French Free 4G connection for my internet (faster than ADSL in my mormot mountains) and I have no problem downloading the files.

Offline

#13 2021-09-28 18:13:51

George
Member
Registered: 2016-04-05
Posts: 142

Re: Problems with TSMEngineManager inside a DLL module

mORMot 2 looks so pretty, and seems, SM engine will be included as well.
In mORMot 1.18, in order to get working SyNode, we must use separate branch.
How it will be implemented in mORMot2 - will the SyNode code appear on the master branch?

Offline

#14 2021-09-28 20:03:20

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

Re: Problems with TSMEngineManager inside a DLL module

Im still on mORMot1 and plan to migrate to mORMot2 in next year.

Offline

Board footer

Powered by FluxBB