Skip to content

Cmajor NodeJS Bindings #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tools/CmajNodeBindings/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
bun.lock
node_modules
62 changes: 62 additions & 0 deletions tools/CmajNodeBindings/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
cmake_minimum_required(VERSION 3.20)


project(cmaj-node-bindings
VERSION 0.1.0
LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)


set(CMAJ_INCLUDE_PLAYBACK 1)
set(CMAJ_VERSION "1.0")


get_filename_component(CMAJOR_ROOT
"${CMAKE_CURRENT_LIST_DIR}/../.." ABSOLUTE)
set(CMAJOR_MODULES_DIR "${CMAJOR_ROOT}/modules")

add_subdirectory(
"${CMAJOR_MODULES_DIR}"
"${CMAKE_BINARY_DIR}/cmj_modules"
EXCLUDE_FROM_ALL)

set(CMAJ_ENABLE_CODEGEN_LLVM_WASM 1)
set(CMAJ_ENABLE_PERFORMER_LLVM 1)
set(CMAJ_ENABLE_CODEGEN_CPP, 1)

message(STATUS "Building Cmajor core library …")
MAKE_CMAJ_LIBRARY(
LIBRARY_NAME cmajor_bindings_cmaj_lib
INCLUDE_PLAYBACK
ENABLE_PERFORMER_LLVM
ENABLE_CODEGEN_LLVM_WASM
ENABLE_CODEGEN_CPP
ENABLE_PERFORMER_INTERPRETER
)


add_library(cmaj-node-bindings SHARED src/binding.cpp)


set(CMAKE_JS_INC ${CMAKE_JS_INC} CACHE INTERNAL "node headers")
set(CMAKE_JS_LIB ${CMAKE_JS_LIB} CACHE INTERNAL "node library")

target_include_directories(cmaj-node-bindings PRIVATE
"${CMAJOR_ROOT}/include"
${CMAKE_JS_INC}
)

target_link_libraries(cmaj-node-bindings PRIVATE
cmajor_bindings_cmaj_lib
${CMAKE_JS_LIB}
)

set_target_properties(cmaj-node-bindings PROPERTIES
PREFIX ""
SUFFIX ".node"
POSITION_INDEPENDENT_CODE ON
)


42 changes: 42 additions & 0 deletions tools/CmajNodeBindings/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { InputEndpoint, OutputEndpoint } from "@cmajor/types";

export class Program {
constructor();
parse(source: string): boolean;
parseAsync(source: string): Promise<boolean>;
reset(): void;
getSyntaxTree(): string | null;
getBinaryModule(): Uint8Array;
}

export class Engine {
constructor();
setBuildSettings(settings: { frequency?: number, sessionID?: number }): void;
getBuildSettings(): { frequency?: number, sessionID?: number }
load(program: Program): boolean;
loadAsync(program: Program): Promise<boolean>;
unload(): void;
link(): void;
linkAsync(): Promise<void>
isLoaded(): boolean;
isLinked(): boolean;
getInputEndpoints(): InputEndpoint[];
getOutputEndpoints(): OutputEndpoint[];
getEndpointHandle(id: string): number;
createPerformer(): Performer;
generateCode(targetType: string, options: Record<string, any>): string;
}

export class Performer {
constructor();
setBlockSize(frames: number): void;
advance(): void;
reset(): void;
getOutputFrames<T extends Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array>(endpointHandle: number, channels: number, outArray?: T): Int32Array;
getOutputEvents(endpointHandle: number): string;
getOutputValue(endpointHandle: number): string;
setInputFrames<T extends Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array>(endpointHandle: number, frames: T): void;
setInputValue(endpointHandle: number, value: any, frames?: number): void;
addInputEvent(endpointHandle: number, event: any): void;
getXRuns(): number;
}
1 change: 1 addition & 0 deletions tools/CmajNodeBindings/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './src/index.js'
35 changes: 35 additions & 0 deletions tools/CmajNodeBindings/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "cmaj-node-bindings",
"version": "0.1.0",
"main": "index.js",
"types": "index.d.ts",
"type": "module",
"license": "GPL-3.0-or-later",
"scripts": {
"build:debug": "cmake-js --debug",
"build": "cmake-js rebuild"
},
"files": [
"index.js",
"index.d.ts",
"build/Release/"
],
"dependencies": {
"@cmajor/types": "../CmajTypes",
"bindings": "^1.5.0",
"cmake-js": "^7.3.1",
"node-addon-api": "^8.3.1",
"typescript": "^5.3.3"
},
"binary": {
"napi_versions": [
4
]
},
"devDependencies": {
"@types/bindings": "^1.5.5",
"@types/node": "^18.19.0",
"node-abi": "^3.52.0",
"typedoc": "^0.25.6"
}
}
36 changes: 36 additions & 0 deletions tools/CmajNodeBindings/src/HandleUtils.h
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just looking at these C++ files and unsure about how much of them is auto-generated.. There are lots of things in here that I'd write a bit differently if I was doing it by hand, but clearly there's been some code-generation involved (but also clearly not all of it is generated..). Is it worth cleaning them up to match our coding style/standards?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There definitely was code generation involved (think I told you that before). I'm not a C++ programmer so I got some help.
It is currently fully functional, but definitely not clean. I wouldn't add it to Cmajor as is - it's a draft / proof of concept. Personally I think it's very useful, but it's up to you to decide if it's worth your time.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once
#define NAPI_EXPERIMENTAL
#include <memory>
#include <napi.h>
#include <string>

template <class T>
using Ptr = std::unique_ptr<T>;

template <class T>
inline Napi::External<Ptr<T>> makeHandle(Napi::Env env, Ptr<T> &&p) {
return Napi::External<Ptr<T>>::New(env, new Ptr<T>(std::move(p)),
[](Napi::Env, Ptr<T> *pp) { delete pp; });
}
template <class T>
inline Napi::External<Ptr<T>> makeHandle(Napi::Env env, T &&value) {
return makeHandle<T>(env,
std::make_unique<T>(std::move(value)));
}

template <class T>
inline T *get(const Napi::CallbackInfo &info, size_t index) {
if (info.Length() <= index || !info[index].IsExternal())
throw Napi::TypeError::New(info.Env(), "argument is not a Cmajor handle");

auto ptr = info[index].As<Napi::External<Ptr<T>>>().Data();
return ptr->get();
}

inline Napi::Object makeResult(Napi::Env env, bool ok, const std::string &msg = {}) {
auto o = Napi::Object::New(env);
o.Set("ok", Napi::Boolean::New(env, ok));
if (!ok)
o.Set("msg", Napi::String::New(env, msg));
return o;
}
Loading