Better HN
topnewbestaskshowjobs
Log inSign up

brendanfh

93 karma · joined Dec 1, 2023 (3y ago) · 15 submissions

Submissions

comment 2y ago · link · parent

The issue right now is how all WebAssembly modules in general are executed. WASM modules currently must contain everything need to run them when they are built. There is no way to dynamically define a new function or variable after they have been built, which would be required for an Onyx REPL to work.

You could definitely use Onyx to create a modern lisp interpreter that can run in the browser without any dependencies, but it might be worth porting an existing C project if that is your goal. I'm not gonna stop you from making something like that in Onyx tho; it would be very cool!

There is a however possibility for a REPL for Onyx to exist outside of the browser. This is because Onyx has another runtime it can use alongside WASM called OVMwasm. This runtime is entirely custom (you can see the source code in the 'interpreter' folder on the Onyx GitHub), so it could be modified to do non-standard things with WASM, like add new functions dynamically. That would take some work to achieve, but in theory should be possible.

comment 2y ago · link · parent

Onyx does not really support a REPL environment at the moment because of the way it is compiled. Its execution cannot be paused in the same way as Python or JS. If you want to give it a try without committing to installing anything, I would recommend the Onyx Playground at https://try.onyxlang.io

comment 2y ago · link · parent

That is a planned feature because I really want that too! I believe it should be possible and not take years to develop, especially because Onyx does have its own WASM runtime that I can completely control how and when certain things happen, like pausing for a hot reload. This runtime is currently Linux only, but I should be able to address that relatively soon. I don't know if it would be possible with Wasmer as the runtime, without Wasmer making severe modifications to its code generation and runtime. Hot reload is a tricky problem, but I believe for the developer experience, it is worth trying.

comment 2y ago · link · parent

I totally agree the auto-advancing carousel was a bad idea. I wanted to add something with movement to the page, but that was definitely not the right idea. The carousel has been removed!

comment 2y ago · link · parent

Thanks for the feedback! I agree my documentation is very lacking in that department. I hope in the next few days to have a page in the documentation that describes my rationale, and the tradeoffs made during development.

comment 2y ago · link · parent

I found it relatively easy. I planned a lot of things out of whiteboards and notebooks, but the actual code writing was mostly straight forward. For Onyx, I went straight from AST to WASM. The trickiest part was probably deciding how non-primitive things would be represented in WASM, like structures and tagged unions. But all of that came later after I had the basics down. I started with the simplest little compiler that could only compile binary operations on integers, and basic function calls. Then I expanded the compiler outward once I had the underlying data structures and code paths figured out.

comment 2y ago · link · parent

That is a good point. I don't do a lot of embedded bare metal work, so I kind of forgot that is possible. I guess it's something that is not unique to Onyx, but a good thing to know nonetheless.

comment 2y ago · link · parent

I haven't directly measured any throughput or latency numbers. I know it is relatively fast, but I know it is not as seamless as I would like.

Currently, for WASM to call any imported function, its arguments have to be formatted in a particular way that is specified by the wasm-c-api. Then it can invoke the imported function. Only after that can the arguments be translated back into the native calling convention in order to call the real native function. I don't know how that compares to node-ffi and JNI, but I imagine its not as slow.

I have a piece of documentation I need to write that will explain how calling native libraries works from Onyx, so that should be able to go into more detail.

comment 2y ago · link · parent

Currently, it does not. But it is written in C with a reasonable separation between the OS things and the compiler things, so compiling with Emscripten should be possible without too much modification.

This is similar to what the Onyx Playground currently does. To do that I just POST your project files to a server, save everything to disk, run the compiler, delete everything, and send the resulting WASM file back to the browser, to be run in a WebWorker.

comment 2y ago · link · parent

That is exactly the way that I see it too.

While Onyx does have core library support for doing standard POSIX things like stdin/stdout/stderr, file operations, and networking, Onyx can be used in an entirely different environment without any of these things. In fact, the standard libraries are (almost) entirely separate from the compiler (i.e. the compiler makes no assumptions about what the standard library is), so if the standard library does not suite your use case for one reason or another, you can write your own. It will be a bit of work, but there would be nothing in your way.

comment 2y ago · link · parent

The example in the blog post could have been written a little bit better, but in Onyx ":=" is actually a combination of two operations: defining a new variable with ':', and assigning the value with '='. If the type can be inferred on the right hand-side of the assignment, you can leave out the type between the ':' and the '=' and it effectively becomes a single operator. An alternative way to write that could have been 'input: str = "111 110 121 120";'.

To assign in Onyx, you only have to use '='.

comment 2y ago · link · parent

The compiler does not have many optimizations currently implemented. Currently, the compilers just uses constant folding and generally reducing unneeded operations if possible. There are plans to do function inlining and better tree-shaking, but those are a ways out.

The "nice" thing about WebAssembly is that the job of generating optimized machine-instructions is up to the WASM embedder, which Wasmer is doing very well right now. Onyx does have a second runtime that is currently only implemented for x86_64 Linux, that does have some more optimizations, but it does not compile to native code; just to an interpreted byte-code.

comment 2y ago · link · parent

The largest project I have in Onyx is currently around 30000 lines (not all that large I know). That project on my Intel i7-1165G7 laptop compiles in about 80ms.

There are currently no large Onyx projects that can really test how this number scales, but I would guess the growth is not linear. So for an off the cuff estimated, I would say a million line project could compile in about 4-5 seconds.

Also worth noting that the entire compiler is currently single-threaded. So that number could get much better.

comment 2y ago · link · parent

Those are all great questions. I will add to the docs to explain more details later, but for now:

1. It is value based, like C. There is obviously passing by pointer, but you do that explcitly.

2/3. Memory is manually managed. To make this easier, like Zig and Odin, everything that allocates should take an allocator as a parameter. This allocator can be the general purpose heap allocator, or it can be an arena, ring buffer, or even tracked allocations that can be freed all at once. More details on that to follow in the documentation.

4. It does have multi-threading support, thanks to the atomics WASM proposal. The standard library currently has support for basic multi-threaded primitives like mutexes, semaphores, and condition variables. Note, there is currently not async/await or any kind of concurrent execution.

5/6. It does have modern type features like sum types. It has Optional(T) and Result(Ok, Err) out of the box. That is the preferred method of error handling.

7. It is mostly statement oriented, but there are features like Elixir's pipe operator (|>) and if/else (value if condition else otherwise) expressions.

comment 3y ago · link · parent

Onyx is entirely independent from Wasmer. It is a separate project developed by me, over the past 3 years. It does however use Wasmer for running on Windows, MacOS, and optionally on Linux.

I started making Onyx because I simply wanted to create my own programming language for the fun of it. I choose WebAssembly as the target for my compiler because it was relatively easy to write my own full-stack solution, instead of relying on something like LLVM. That language evolved over the years until I had something I felt was worth sharing. When the Wasmer team reached out to see if I wanted to do a collaboration blog post, I said yes.

The main reason I didn't go with LLVM was simply because I wanted to write everything myself (for fun) and to have a super-fast compiler. LLVM is not the fastest backend, but it is understandable for all the optimizations that it does.