tondrej / chakracore-delphi

Delphi and Free Pascal bindings and classes for Microsoft's ChakraCore library
https://tondrej.blogspot.com/search/label/chakracore
MIT License
136 stars 34 forks source link

Multiple Instances & Prototype Objects #2

Closed reddor closed 6 years ago

reddor commented 6 years ago

When creating a second ChakraCore-runtime, it is not possible to register a prototype object that also was registered in the first runtime. Pseudo-Code:

procedure TMyThread.Execute;
begin
  Runtime:=TChakraCoreRuntime.Create();
  Context:=TChakraCoreContext.Create(Context);
  Context.Activate;
  TConsole.Project('Console');
end;

As soon as a second instance is created, an EChakraCoreFatal exception is created with "A hosting API was called with object created on different javascript runtime". Seen with FreePascal 3.x on x64-linux.

My workaround for this is in ChakraCoreClasses.pas, declaring Lock and ProjectedClasses as threadvar, and calling InitializeProjectedClasses/FinalizeProjectedClasses in the constructor/destructor of TChakraCoreRuntime.

Apart from that: very nice work!

tondrej commented 6 years ago

I admit I've never tried using multiple runtimes (or destroying one and creating a new one). Good catch. I'll look into it. Thank you for your feedback!