eanders-ms / pxtdll-vs-build

Proof of concept to build pxt.dll using Visual Studio
1 stars 0 forks source link

Nice job! Can we run this as a TsRuntime? #1

Open linsmod opened 9 months ago

linsmod commented 9 months ago

I'm wondering to porting the pxt runtime into a mini browser to act as a scriptEngine like js, because the mini browser has no js support currently...

Before I saw you project, I built the pxtVM success but I have no payload can be run.

I noticed the compiler is written by Typescript and have a deep binding to PXT, I'm not familar with the typescript lang, It's absolutly not friendly to me if I want split it out from PXT.

What was I tried run:

R7K:/work/build-tsvm/tsvm$ ./tsvm binary.js 
[       0] loading image at 0x7fbdd62d0010 (1242577 bytes)
[       9] validation error 1001 at 0x0
R7K:/work/build-tsvm/tsvm$ 

Do you know how to let a compiler to compile the typescript codes to the vm playload format?

eanders-ms commented 9 months ago

Interesting use case! I think the most help I can be here is to steer you in another direction. The TypeScript supported by pxt is a tiny subset of the actual language, and doesn't implement basic APIs like Object.defineProperty. Have you considered trying QuickJS? It is quite nice, and friendly to embedded scenarios. https://bellard.org/quickjs/, https://github.com/bellard/quickjs

linsmod commented 9 months ago

Yes, I did think about using QuickJS, its a small and clear lib benefits browser-like projects, but I chose pxt finnaly. One reason I chose Typescript is because I prefer Strong Type programming language, and the Typescript subset used by pxt fits my requirements: StrongTyped, clearly and simple Type System suite for small devices.

Another reason, some guys always like to do things that have not been done by somebody yet, like me ^^.

eanders-ms commented 9 months ago

I did one other experiment with pxt.dll, instantiating a pxt-arcade game within a Unity project: https://github.com/eanders-ms/arcade-unity-demo. I may be informative to see how the dll was instantiated, via C# P/invoke, in this case: https://github.com/eanders-ms/arcade-unity-demo/blob/main/Assets/Plugins/pxt/Pxt.cs

One thing to note is that pxt.dll is a runtime, not a compiler. It cannot load JavaScript, only compiled binaries.

How I managed to get a compiled binary for the dll was not straightforward. I believe it involved running one of the pxt targets (pxt-arcade or pxt-microbit) on localhost and tweaking the code to force it to build the dll's format. There is probably better way, but I don't know it.

linsmod commented 9 months ago

Funny experiment!

var rom = Resources.Load<TextAsset>("roms/castle-crawler.pxt64");
        VmStartBuffer(rom.bytes, rom.bytes.Length);

As you said, the way to get the rom(actually generated by a ts-to-vmBytecode compilier, and the compiler is implemented by typescript) and the runtime(pxt.dll) needs some tweaking, about , like choosing the 'dockermake' or 'arcadedocker' targets or changing the build parameter to force local build,, --forceLocal. I need to go back to review the PXT or PXT-Arcade's code to get more insights.