BunSharp 0.1.2

dotnet add package BunSharp --version 0.1.2
                    
NuGet\Install-Package BunSharp -Version 0.1.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BunSharp" Version="0.1.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BunSharp" Version="0.1.2" />
                    
Directory.Packages.props
<PackageReference Include="BunSharp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add BunSharp --version 0.1.2
                    
#r "nuget: BunSharp, 0.1.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package BunSharp@0.1.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=BunSharp&version=0.1.2
                    
Install as a Cake Addin
#tool nuget:?package=BunSharp&version=0.1.2
                    
Install as a Cake Tool

<div align="center"> <img src="assets/BunSharp.png" width="120" alt="BunSharp" />

BunSharp

NuGet .NET License: LGPL v2.1

</div>

BunSharp is a .NET binding for the libbun embed API (libbun based on (Bun). It lets you create a Bun runtime, execute JavaScript or TypeScript, and export C# types into the JS environment.

Features

  • Evaluate JavaScript and TypeScript from .NET
  • Register host functions on the JS global object
  • Export C# classes with JSExportAttribute
  • Support instance methods, instance properties, static members, and byte[]Uint8Array
  • Support T[] arrays as parameters and return values — including nested arrays (T[][]) and arrays of exported classes
  • No runtime reflection — AOT friendly

Requirements

  • .NET 10.0 or later
Platform Architecture Supported
Windows x64
Linux x64
macOS arm64

Installation

dotnet add package BunSharp

Or in your project file:

<ItemGroup>
  <PackageReference Include="BunSharp" Version="x.y.z" />
</ItemGroup>

BunSharp automatically pulls in BunSharp.Generator as a Roslyn analyzer. No additional setup is required for JSExport to work.

Explicit Generator Configuration

If you need to pin the generator version independently from the main package, exclude the bundled analyzer and add BunSharp.Generator directly:

<ItemGroup>
  <PackageReference Include="BunSharp" Version="x.y.z" ExcludeAssets="analyzers" />
  <PackageReference Include="BunSharp.Generator" Version="x.y.z" PrivateAssets="all" />
</ItemGroup>

Note: If analyzers are disabled (e.g. via <EmitCompilerGeneratedFiles>false</EmitCompilerGeneratedFiles>) without the explicit reference above, JSExport will not generate the necessary glue code and compilation will fail.

Basic Usage

using BunSharp;

using var runtime = BunRuntime.Create();
var context = runtime.Context;

context.Evaluate("globalThis.answer = 1 + 1;");

var result = context.GetProperty(context.GlobalObject, "answer");
Console.WriteLine(context.ToInt32(result));

Export a C# Class to JavaScript

using BunSharp;

[JSExport]
public sealed class DemoGreeter
{
	public DemoGreeter(string name, byte[] payload)
	{
		Name = name;
		Payload = payload;
	}

	public string Name { get; set; }

	public byte[] Payload { get; set; }

	public string describe()
	{
		return $"{Name}:{Payload.Length}";
	}

	public static string Version => "v1";

	[JSExport(false)]
	public string Hidden => "hidden";
}

using var runtime = BunRuntime.Create();
var context = runtime.Context;

context.ExportType<DemoGreeter>();

var value = context.EvaluateExpression(@"(() => {
	const greeter = new DemoGreeter('Ada', new Uint8Array([1, 2, 3, 4]));
	return `${greeter.describe()}|${greeter.name}|${greeter.payload.length}|${DemoGreeter.version}`;
})()");

Console.WriteLine(context.ToManagedString(value));
// Ada:4|Ada|4|v1

JSExport Rules

[JSExport]          // enable export
[JSExport(true)]    // same as above
[JSExport("name")]  // enable export and override the JS name
[JSExport(false)]   // disable export
  • Apply JSExport to a class to export it
  • Exported classes automatically include all public instance and static methods and properties
  • Class names stay unchanged by default
  • Method and property names are exported as camelCase by default
  • JSExport(false) on a member excludes that member from export

Arrays

T[] is supported wherever any other type is supported: constructor parameters, method parameters, return values, and properties. Supported element types: bool, int, double, string, byte[], BunValue, any [JSExport] class, and nested arrays.

A JS Array is mapped to a C# T[]; null / undefined maps to null.

[JSExport]
public sealed class DataService
{
    public DataService() { }

    // string[] ↔ JS Array of strings
    public string[] reverseNames(string[] names)
    {
        Array.Reverse(names);
        return names;
    }

    // [JSExport] class array
    public DemoGreeter[] makeGreeters(string[] names)
        => names.Select(n => new DemoGreeter(n, [])).ToArray();

    // nested array
    public string[][] transpose(string[][] matrix) { /* ... */ }

    // static property
    public static string[] Tags => ["fast", "aot", "ts"];
}
const svc = new DataService();
console.log(svc.reverseNames(["a", "b", "c"]));			// ["c", "b", "a"]
console.log(svc.makeGreeters(["Alice", "Bob"])[0].describe());	// Alice:0
console.log(DataService.tags);								// ["fast", "aot", "ts"]

Note: byte[] always maps to Uint8Array via a zero-copy path and is independent of the general T[] mechanism.

Host Functions

using BunSharp;

using var runtime = BunRuntime.Create();
var context = runtime.Context;

var hello = context.CreateFunction(
	"helloFromDotNet",
	static (ctx, args, _) =>
	{
		var name = args.Length > 0 ? ctx.ToManagedString(args[0]) : "world";
		return ctx.CreateString($"Hello, {name}, from .NET.");
	},
	argCount: 1);

context.SetProperty(context.GlobalObject, "helloFromDotNet", hello);
context.Evaluate("console.log(helloFromDotNet('Bun')); ");

Contributing

Bug reports and pull requests are welcome on GitHub. Please open an issue before submitting large changes.

License

LGPL-2.1

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.2 40 4/9/2026
0.1.1 36 4/8/2026
0.1.0 79 3/31/2026