-
Notifications
You must be signed in to change notification settings - Fork 34
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
Mudloop
wants to merge
4
commits into
cmajor-lang:main
Choose a base branch
from
Mudloop:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.DS_Store | ||
bun.lock | ||
node_modules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './src/index.js' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.