Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
QuickJS, the Next Generation: a mighty JavaScript engine (github.com/quickjs-ng)
66 points by jcbhmr on July 8, 2024 | hide | past | favorite | 40 comments


QuickJS is presumably intended for embedding within applications - how does it compare to Lua for this?

Both languages are fairly quirky - at least JavaScript’s quirks are widely-known. But how do the implementations compare in terms of API, robustness, portability, performance etc?

Also, I’m not aware of any static typing add-ons for Lua, but presumably it’s possible to use TypeScript with QuickJS?


> ...but presumably it’s possible to use TypeScript with QuickJS?

I don't see why not, since TS compiles down to JS. The engine doesn't know or care about what kind of transformations the code went through before it was JS. You might need to be careful about compatibility, the QuickJS intro says 'Almost complete ES2023 support", so you'd have to avoid any unsupported features.


Teal (https://github.com/teal-language/tl) is the closest we have from a "TypeScript for Lua". It is natively supported by many projects now, including game engines such as the Bevy scripting plugin and Defold.


Oh, interesting. Thanks!


> Also, I’m not aware of any static typing add-ons for Lua

I'd not cal TypeScript an addon to JS. It's a language that transpiles to JS (and so does CoffeScript, Elm, etc. etc.)

There are many language projects that transpile to Lua:

https://github.com/hengestone/lua-languages?tab=readme-ov-fi...


On the other hand, my understanding is that the transformations the TS compiler does to the code is just stripping annotations, so it's not unreasonable to call it an add-on.


Sure call it what you like. Just know that it's very different from a Firefox add-on.


It’s a strict superset


Thanks, there are many more there than I expected!


Luau is a gradually typed version of Lua at least.


I have been using TypeScript for quite sometime but it's my first time learning of one gradually typed language. I always decribed TypeScript as optionally statically typed


Doesn’t Luau require a custom Lua implementation though? Is that implementation easily embeddable like the regular Lua VM?


Yes, and yes. There are libraries for embedding lua in rust programs that allow you to trivially switch between Lua, LuaJIT, and Luau.


As the title & link suggest, this project is a fork from QuickJS, a Javascript originally developed by Fabrice Bellard (https://bellard.org/quickjs/).

The fork is apparently in the process of re-integrating with upstream, after work on QuickJS resumed.

From the QuickJS-ng README:

> In October 2023 @bnoordhuis and @saghul decided to fork the QuickJS project

> ...

> As of early 2024 both projects agree the proper path forward involves merging both projects and combining the efforts.


Related ongoing thread:

Show HN: Execute JavaScript in a WebAssembly QuickJS sandbox - https://news.ycombinator.com/item?id=40896873 - July 2024 (47 comments)


What's the current state of QuickJS? Is there anywhere it's a clearly better choice? Does it have a shot of being a top class engine?


From my observations: cold start, ease of patching. If you're running a lot of different JS code or restarting the code frequently, it's faster than node. Where it's useful: fuzzing. If you have a library/codebase you want to fuzz, you need to restart the code from a snapshot, and other engines seem to do it slower. It's also really easy to patch the code, because of the codebase size. If you need to trace/observe some behavior, just do it.


> Is there anywhere it's a clearly better choice?

Embeddable use when you need latest ECMAScript support.

> Does it have a shot of being a top class engine?

No.


Why does it not have a shot of being a top class engine?


Because the main goals are to be small, embeddable, and have low startup time.

A top class engine requires a JIT and that generally leads to being large and having high startup time. LuaJIT is pretty much the exception which proves the rule and 1. Mike Pall is a monster of a developer not unlike Bellard; and 2. Pall is on record (in discussions with other JIT people) that in his opinion you can’t bolt a JIT to an existing runtime and get a maximally efficient JIT. So you’d need someone of that class of skill to decide to make that their legacy.


And it's not like that Bellard is unable to produce a JIT compiler; see, for example, QEMU dyngen [1] which eventually inspired CPython's new JIT compiler.

[1] https://www.usenix.org/legacy/event/usenix05/tech/freenix/fu...


Like other replies said, having JIT requires quite different tradeoffs to be made.

And unfortunately, making a very fast interpreter-based JS engine is a tedious and human-power-heavy work, it involves a lot of work which can't be eliminated by better skills.


Performance


Apparently Figma still uses it for plugins, so there's that.

It can be compiled to WebAssembly and run with the sandboxing of WebAssembly. Not just data sandboxing but sandboxing that can prevent it from slowing down the rest of the app (though it runs slower).

Post from Figma in 2019: https://www.figma.com/es-la/blog/an-update-on-plugin-securit...

This in 2023: "Either Figma hasn't updated QuickJS since, or they've moved to a different VM, because Figma's VM is missing many of the features QuickJS now supports." https://www.sam.today/blog/bumbling-the-figma-api


Release notes marking differences from the original would be nice. I diffed the main header file, and at least public API seems quite similar.

Even if I wanted to switch, I just don't know what I'd be running into, until I do. :)


For a while quickjs-ng was ahead in Ecmascript language features implemented but now they are more even again.


What is mighty about QuickJS? It would be nice if they included how this marketing-esque term connects with the product at a src-code-level.


Can someone help me understand why there is no HTTP fetch built into this?

JavaScript is great but without HTTP fetch alot of potential uses are off the table.


fetch is a WHATWG standard, not ECMAScript. Qjs devs have made a language interpreter and not a scripting environment. You can use txiki [1] for that, it has fetch.

[1] https://github.com/saghul/txiki.js


I think because it runs inside a WebAssembly sandbox, but maybe someone with hands on experience can explain what's necessary to make calls outside that (if it's possible at all).


This was written by Fabrice Bellard. I'm curious to know WHY he wrote it. Does anyone know?


given that it's fabrice bellard we're talking about, I suspect the answer is "because he could"!


I would be careful to use this in production. I'm a diehard C and C++ fan, but: Having a mutex unlock inside an if statement that performs an early return, on line 52000 something, does not inspire confidence.

Excellent job on building this, and it's very readable and nice, otherwise!


Line 52,000 and x of what file?

I didn't find any file with that many line's yet, still searching but I doubt it exists unless it's in a different repository.



Thanks for digging that up.

It looks like the mutex is theoretically sound.. unlocked before all returns, as long as there isn't an exception raised in between. I wonder what the parent comment's issue is.


Oh yeah of course its sound, but this is not how you write C code, because all it takes is for someone else in 10 years to, somewhere between 51000 and 53000 introduce an early return statement and forget to unlock that mutex, or other little things.

This is solved in C usually with a goto to a cleanup routine, and avoiding early returns.


This is C, not C++ - very different languages.


Yes, you should be using a goto for this in C


when I see C/C++ code, I prefer do not run it.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: