Coder Social home page Coder Social logo

tondrej / chakracore-delphi Goto Github PK

View Code? Open in Web Editor NEW
136.0 27.0 34.0 607 KB

Delphi and Free Pascal bindings and classes for Microsoft's ChakraCore library

Home Page: https://tondrej.blogspot.com/search/label/chakracore

License: MIT License

C 32.75% Pascal 66.13% Shell 1.13%
chakracore microsoft javascript-engine delphi freepascal lazarus javascript javascript-tools

chakracore-delphi's Introduction

chakracore-delphi

Licensed under the MIT License PR's Welcome

Delphi and Free Pascal bindings and classes for Microsoft's ChakraCore library.

ChakraCore version: 1.11.24

Supported compilers:

  • Delphi 7 or newer
  • Free Pascal 3.0.4 or newer

Supported target platforms:

  • i386-win32 (Delphi 7 or newer, Free Pascal)
  • x86_64-win64 (Delphi XE2 or newer, Free Pascal)
  • x86_64-linux (Free Pascal)
  • x86_64-darwin (Free Pascal)

Installation:

  1. Clone this repository:
git clone https://github.com/tondrej/chakracore-delphi.git
cd chakracore-delphi
git submodule update --init

or

git clone --recurse-submodules https://github.com/tondrej/chakracore-delphi.git
  1. Download the binaries from the ChakraCore Release page
  2. Enjoy!

chakracore-delphi's People

Contributors

tondrej avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

chakracore-delphi's Issues

EAccessViolation using Variants branch

First, I'd like to thank you for such a great work.

Regarding the EAccessViolation:

I'm compiling using barebones freepascal on Windows:
Free Pascal Compiler version 3.2.0 [2020/06/04] for i386

Windows 10 Pro
20H2 Build 19042.685

Using code from the gist from in your October post (https://tondrej.blogspot.com/2020/10/variant-support-in-chakracore-delphi.html)

I have added a writeln at the end:

  ...
  finally
    VarClear(MyObj);
    VarClear(Json);
    Context.Free;
    Runtime.Free;
  end;
  WriteLn('After freeing.');
end.

Code runs ok:

Hello, world!
{"name":"world"}
After freeing.

But throws exceptions afterwards:

An unhandled exception occurred at $732ECF2F:
EAccessViolation: Access violation
  $732ECF2F
  $00440BF6
  $0041DF78
  $00415DF6
  $00412833

An unhandled exception occurred at $732ECF2F:
EAccessViolation: Access violation
  $732ECF2F
  $00440BF6
  $0041DF78
  $00415DF6
  $00412833
  $0040D959
  $76F771E2
  $76F771B4
  $76F63B36
  $00440BF6
  $0041DF78
  $00415DF6
  $00412833

String conversion error

empty string conversion error

function StringToJsString(const S: UnicodeString): JsValueRef;

var
s: JsValueRef;
begin
s := StringToJsString(UTF8String('')); //passed
s := StringToJsString('');//this error
end;

Call to PostTimedTask fails on Windows (x86_64) in HostSample

Say we run the following script in the HostSample project:

setTimeout(() => { console.log("(timeout 1000) Current time: ", new Date()) });
setTimeout(() => { console.log("(timeout 1000) Current time: ", new Date()) }, 1000);
setTimeout(c => { c.log("(timeout 1000) Current time: ", new Date()) }, 1000, console);

Such a script will run fine on Windows (i386-win32). However, it will fail on Windows (x86_64) with the following error:

[EAccessViolation] Access violation at address 00007FFBE7308B30 in module 'chakracore.dll'. Read of address 0000000000000010

This issue was observed on Delphi 10.1 Berlin. It seems like this issue happens in the PostTimedTask method because the FuncArgs variable is declared as an array[0..0] of JsValueRef instead of an array of JsValueRef. I was able to fix the issue by changing the PostTimedTask method like so:

function PostTimedTask(Args: PJsValueRefArray; ArgCount: Word; CallbackState: Pointer; RepeatCount: Integer): JsValueRef;
var
  DataModule: TDataModuleMain absolute CallbackState;
  AMessage: TTaskMessage;
  Delay: Cardinal;
  FuncArgs: array of JsValueRef;
  I: Integer;
begin
  Result := JsUndefinedValue;

  if ArgCount < 2 then // thisarg, function to call, optional: delay, function args
    raise Exception.Create('Invalid arguments');

  if ArgCount >= 3 then
    Delay := JsNumberToInt(Args^[2])
  else
    Delay := 0;

  if ArgCount >= 4 then
  begin
    SetLength(FuncArgs, ArgCount - 3);
    for I := 0 to ArgCount - 4 do
      FuncArgs[I] := Args^[I + 3];
  end;

  AMessage := TTaskMessage.Create(DataModule.Context, Args^[1], Args^[0], FuncArgs, Delay, RepeatCount);
  try
    DataModule.Context.PostMessage(AMessage);
  except
    AMessage.Free;
    raise;
  end;
end;

I see that the PostTimedTask method may need to be fixed in the other sample projects.

My fix would also need to be tested on the FreePascal compiler and on the Linux and Darwin platforms.

Let me know your thoughts and if you are open to me making further tests and creating a PR.

nodesample

Please give a sample script of nodesample.exe.
nodesample.exe demo_lodash.js
I'm going to use that to prompt:


Debugger Exception Notification

Project NodeSample.exe raised exception class Exception with message 'Module 'lodash' not found'.

Break Continue Help

Delphi compiles to Linux

It is recommended to remove InitCriticalSection and DoneCriticalSection

Changing compat. pas allows Delphi to support Linux!

Rtti-based binding

Hello

I partially implemented Delphi and Lua binding based on RTTI and manually registered information. See an example, I hope you like this way: https://github.com/d-mozulyov/CrystalLUA/blob/master/README.md

It seems to me that it would be useful to bind Delphi code not only with Lua, but also with other languages: JavaScript, Python, etc. But since it is quite difficult to process RTTI, and it is very different for FreePascal, old and new versions of Delphi, I have created several units that can be used now: https://github.com/d-mozulyov/Tiny.Library/blob/master/doc/Rtti.md

You can contact me with any questions. I would like to see a convenient Delphi+JS library appear over time.

The hosting API requires that a context be current, but there is no current context

Hi,

When running multiple instances on a service application. In general, I evaluate a script at creation time, and that works fine. Base on external events (TCP packages), I call a function within the script using JsGetProperty to get a handle of the function, and then CallFunction passing the function handle.

When running this on a standard form-based application, it works perfectly. However when compiling the same code on a service application I get the 'The hosting API requires that a context be current, but there is no current context' error when calling JsGetProperty.

note: when testing the packages on a form application I used a single edit field and button to trigger the event. I'm not sure if the problem is because of a service application, or because the triggered events could be from any random thread. Because of this, I tried to first call JsSetCurrentContext(FContext.handle); before, but this one actually returns 'JsErrorWrongThread'.

Any suggestion is more than welcome! :)

Delphi-Win64
last update on source was yesterday night.

TMethod(NativeMethod).Data is nil

TMethod(NativeMethod).Code := CallbackState;
TMethod(NativeMethod).Data := JsGetExternalData(Args^[0]);

//Can we add a judgment here?
if TMethod(NativeMethod).Data=nil then
  raise Exception.Create('Data is null.');

//If you execute the wrong js code, Data is nil
if Args^[0] <> TNativeObject(TMethod(NativeMethod).Data).Instance then
  raise Exception.Create('thisarg not the registered instance');

Eval

How can we un code and give back a result? Like eval, for example
2 + 3; run as a code and return 5

Need an example

Hey there. Can you show an example to run some "hello world" easy script? Thanks!

Multiple Instances & Prototype Objects

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!

Tests/Samples fail to compile - JsDeleteProperty not marked with overload

Adding the function overloads to the interface portion seems to correct it:

function JsDeleteProperty(Value, Prop: JsValueRef; UseStrictRules: Boolean): Boolean; overload;
function JsDeleteProperty(Value: JsValueRef; PropId: JsPropertyIdRef; UseStrictRules: Boolean): Boolean; overload;
function JsDeleteProperty(Value: JsValueRef; const PropName: UTF8String; UseStrictRules: Boolean): Boolean; overload;
function JsDeleteProperty(Value: JsValueRef; const PropName: UnicodeString; UseStrictRules: Boolean): Boolean; overload;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.