Skip to content

uwx/Hermes.Net

Repository files navigation

Hermes.Net

.NET bindings for Hermes, Facebook's JavaScript engine optimised for React Native.

Quick start

using Hermes.Net;

// 1. Create a runtime
using var config = new HermesRuntimeConfig { EnableEval = true };
using var runtime = new HermesRuntime(config);

// 2. Use CheckedHermesRuntime for exception-throwing convenience wrappers
var crt = new CheckedHermesRuntime(runtime);

// 3. Evaluate JavaScript
using var buf = new HermesBufferImpl("1 + 2"u8.ToArray());
using var result = crt.EvaluateJavascriptSource(buf, "script.js");

// 4. Read the result
result.TryGetValue(out double value);  // value == 3.0

Implementing a host object

class Counter : IHermesHostObject
{
    private int _count;

    public HermesValueOrError Get(CheckedHermesRuntime rt, HermesPropNameID prop)
    {
        // return the current count for any property access
        return new HermesValueOrError(HermesValue.FromDouble(_count));
    }

    public HermesABIVoidOrError Set(CheckedHermesRuntime rt, HermesPropNameID prop, in HermesValue value)
    {
        if (value.TryGetValue(out double d)) _count = (int)d;
        return default;
    }

    public HermesPropNameIDListOrError GetOwnKeys(CheckedHermesRuntime rt) => default;

    public void Dispose() { }
}

// Register with the runtime
using var hostObject = new HermesHostObjectImpl(new Counter());
crt.SetObjectProperty(globalObject, nameProp, crt.CreateObjectFromHostObject(hostObject));

Exposing a host function

TBA (No fancy managed API for it yet)

Implementation details

Hermes.Net exposes a safe, managed wrappers to a slightly enhanced version of the Hermes C ABI (hermes_abi.h), with some extra functions based on jsi.h (for instance, so you can use UTF-16 directly without encoding). The fork should be relatively easy to keep synced to upstream. In the future, I might provide a version of the library that works with the plain unmodified ABI.

Because the ABI is almost a one-to-one mapping of the JSI API, it is possible to create an ABI wrapper for any JS engine that implements JSI, so in theory this library could become a one-size-fits-all wrapper for JavaScriptCore, V8, etc.

Memory management

  • You can call Dispose on HermesValue & adjacents if you want or let the runtime destroy values on finalization.
  • Do not call release() on the vtable, we manage it for you.
  • CheckedHermesRuntime does not own the underlying HermesRuntime; dispose the runtime separately.
  • If you dispose of the runtime you should always dispose the HermesValues you created, otherwise you might get the finalizers trying to free a broken pointer (the C# side of the runtime needs to keep weakrefs to all values it creates, this will be fixed later).

Building

Requires the .NET 11 SDK and the pre-built hermes.dll in Hermes.Net/.

dotnet build
dotnet test

We have a Windows x64 Debug hermes.dll in the repo, but you should build and bring your own, especially since the PDB is far too large to check in.

License

MIT. See hermes/LICENSE for the Hermes engine license.

About

Performant Hermes bindings for .NET

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors