From 064e7caceefab19582050f7225b5158fb21c7357 Mon Sep 17 00:00:00 2001 From: Saksham Goyal Date: Fri, 31 Oct 2025 18:17:13 -0400 Subject: [PATCH] Official node addons api instead of v8 mess --- package-lock.json | 48 +++++++++++---------- packages/muray/binding.gyp | 4 ++ packages/muray/muray.cpp | 84 ++++++++++++++++++++----------------- packages/muray/package.json | 5 ++- 4 files changed, 80 insertions(+), 61 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7bd12f3..71747e2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -51,7 +51,6 @@ "integrity": "sha512-30iZtAPgz+LTIYoeivqYo853f02jBYSd5uGnGpkFV0M3xOt9aN73erkgYAmZU43x4VfqcnLxW9Kpg3R5LC4YYw==", "dev": true, "license": "Apache-2.0", - "peer": true, "dependencies": { "@jridgewell/gen-mapping": "^0.3.5", "@jridgewell/trace-mapping": "^0.3.24" @@ -118,7 +117,6 @@ "integrity": "sha512-60X7qkglvrap8mn1lh2ebxXdZYtUcpd7gsmy9kLaBJ4i/WdY8PqTSdxyA8qraikqKQK5C1KRBKXqznrVapyNaw==", "dev": true, "license": "MIT", - "peer": true, "engines": { "node": ">=6.9.0" } @@ -160,8 +158,7 @@ "resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-2.0.0.tgz", "integrity": "sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==", "dev": true, - "license": "MIT", - "peer": true + "license": "MIT" }, "node_modules/@babel/generator": { "version": "7.28.0", @@ -199,7 +196,6 @@ "integrity": "sha512-2+1thGUUWWjLTYTHZWK1n8Yga0ijBz1XAhUXcKy81rd5g6yh7hGqMp45v7cadSbEHc9G3OTv45SyneRN3ps4DQ==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "@babel/compat-data": "^7.27.2", "@babel/helper-validator-option": "^7.27.1", @@ -241,7 +237,6 @@ "integrity": "sha512-dSOvYwvyLsWBeIRyOeHXp5vPj5l1I011r52FM1+r1jCERv+aFXYk4whgQccYEGYxK2H3ZAIA8nuPkQ0HaUo3qg==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "@babel/helper-module-imports": "^7.27.1", "@babel/helper-validator-identifier": "^7.27.1", @@ -300,7 +295,6 @@ "integrity": "sha512-/V9771t+EgXz62aCcyofnQhGM8DQACbRhvzKFsXKC9QM+5MadF8ZmIm0crDMaz3+o0h0zXfJnd4EhbYbxsrcFw==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "@babel/template": "^7.27.2", "@babel/types": "^7.28.2" @@ -1122,8 +1116,7 @@ "url": "https://github.com/sponsors/ai" } ], - "license": "CC-BY-4.0", - "peer": true + "license": "CC-BY-4.0" }, "node_modules/chalk": { "version": "4.1.2", @@ -1556,8 +1549,7 @@ "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.192.tgz", "integrity": "sha512-rP8Ez0w7UNw/9j5eSXCe10o1g/8B1P5SM90PCCMVkIRQn2R0LEHWz4Eh9RnxkniuDe1W0cTSOB3MLlkTGDcuCg==", "dev": true, - "license": "ISC", - "peer": true + "license": "ISC" }, "node_modules/elliptic": { "version": "6.6.1", @@ -1621,7 +1613,6 @@ "integrity": "sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA==", "dev": true, "license": "MIT", - "peer": true, "engines": { "node": ">=6" } @@ -1757,7 +1748,6 @@ "integrity": "sha512-3hN7NaskYvMDLQY55gnW3NQ+mesEAepTqlg+VEbj7zzqEMBVNhzcGYYeqFo/TlYz6eQiFcp1HcsCZO+nGgS8zg==", "dev": true, "license": "MIT", - "peer": true, "engines": { "node": ">=6.9.0" } @@ -2337,7 +2327,6 @@ "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==", "dev": true, "license": "MIT", - "peer": true, "bin": { "json5": "lib/cli.js" }, @@ -2408,7 +2397,6 @@ "integrity": "sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==", "dev": true, "license": "ISC", - "peer": true, "dependencies": { "yallist": "^3.0.2" } @@ -2582,13 +2570,27 @@ "resolved": "packages/murayact", "link": true }, + "node_modules/node-addon-api": { + "version": "8.5.0", + "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-8.5.0.tgz", + "integrity": "sha512-/bRZty2mXUIFY/xU5HLvveNHlswNJej+RnxBjOMkidWfwZzgTbPG1E3K5TOxRLOR+5hX7bSofy8yf1hZevMS8A==", + "license": "MIT", + "engines": { + "node": "^18 || ^20 || >= 21" + } + }, + "node_modules/node-api-headers": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/node-api-headers/-/node-api-headers-1.6.0.tgz", + "integrity": "sha512-81T99+mWLZnxX0LlZPYuafyFlxVVaWKQ0BDAbSrOqLO+v+gzCzu0GTAVNeVK8lucqjqo9L/1UcK9cpkem8Py4Q==", + "license": "MIT" + }, "node_modules/node-releases": { "version": "2.0.19", "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.19.tgz", "integrity": "sha512-xxOWJsBKtzAq7DY0J+DTzuz58K8e7sJbdgwkbMWQe8UYB6ekmsQ45q0M/tJDsGaZmbC+l7n57UV8Hl5tHxO9uw==", "dev": true, - "license": "MIT", - "peer": true + "license": "MIT" }, "node_modules/normalize-path": { "version": "3.0.0", @@ -2987,6 +2989,7 @@ "resolved": "https://registry.npmjs.org/react/-/react-18.3.1.tgz", "integrity": "sha512-wS+hAgJShR0KhEvPJArfuPVN1+Hz1t0Y6n5jLrGQbkb4urgPE/0Rve+1kMB1v/oWgHgm4WIcV+i7F2pTVj+2iQ==", "license": "MIT", + "peer": true, "dependencies": { "loose-envify": "^1.1.0" }, @@ -3175,7 +3178,6 @@ "integrity": "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==", "dev": true, "license": "ISC", - "peer": true, "bin": { "semver": "bin/semver.js" } @@ -3712,7 +3714,6 @@ } ], "license": "MIT", - "peer": true, "dependencies": { "escalade": "^3.2.0", "picocolors": "^1.1.1" @@ -3830,13 +3831,16 @@ "resolved": "https://registry.npmjs.org/yallist/-/yallist-3.1.1.tgz", "integrity": "sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==", "dev": true, - "license": "ISC", - "peer": true + "license": "ISC" }, "packages/muray": { "version": "0.0.1", "hasInstallScript": true, - "license": "MIT" + "license": "MIT", + "dependencies": { + "node-addon-api": "*", + "node-api-headers": "*" + } }, "packages/murayact": { "version": "0.0.1", diff --git a/packages/muray/binding.gyp b/packages/muray/binding.gyp index 612380f..8043f69 100644 --- a/packages/muray/binding.gyp +++ b/packages/muray/binding.gyp @@ -7,6 +7,9 @@ 'vendor/microui.c' ], 'include_dirs': ['vendor/'], + 'dependencies': [ + " #endif -#include +#include #include extern "C" { #include "microui.h" @@ -14,42 +14,42 @@ static mu_Context ctx = {0}; static char input_buf[128]; static mu_Rect unclipped_rect = { 0, 0, 0x1000000, 0x1000000 }; -void MuButton(const v8::FunctionCallbackInfo &args) { - auto isolate = args.GetIsolate(); - auto context = isolate->GetCurrentContext(); - bool result = mu_button(&ctx, *v8::String::Utf8Value(isolate, args[0]->ToString(context).ToLocalChecked())); - args.GetReturnValue().Set(v8::Boolean::New(isolate, result)); +Napi::Value MuButton(const Napi::CallbackInfo &args) { + auto env = args.Env(); + bool result = mu_button(&ctx, args[0].ToString().Utf8Value().c_str()); + return Napi::Boolean::New(env, result); } -void MuLabel(const v8::FunctionCallbackInfo &args) { - auto isolate = args.GetIsolate(); - auto context = isolate->GetCurrentContext(); - auto label = *v8::String::Utf8Value(isolate, args[0]->ToString(context).ToLocalChecked()); - mu_label(&ctx, *v8::String::Utf8Value(isolate, args[0]->ToString(context).ToLocalChecked())); +Napi::Value MuLabel(const Napi::CallbackInfo &args) { + auto env = args.Env(); + mu_label(&ctx, args[0].ToString().Utf8Value().c_str()); + return env.Undefined(); } -void MuInput(const v8::FunctionCallbackInfo &args) { - auto isolate = args.GetIsolate(); - auto context = isolate->GetCurrentContext(); +Napi::Value MuInput(const Napi::CallbackInfo &args) { + auto env = args.Env(); int val = mu_textbox(&ctx, input_buf, sizeof(input_buf)); if (val & MU_RES_CHANGE) { mu_set_focus(&ctx, ctx.last_id); - args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, input_buf).ToLocalChecked()); + return Napi::String::New(env, input_buf); } + return env.Undefined(); } -void MuBeginWindow(const v8::FunctionCallbackInfo &args) { +Napi::Value MuBeginWindow(const Napi::CallbackInfo &args) { mu_begin_window(&ctx, "Murayact", mu_rect(20, 20, 300, 300)); const int widths[] = { 300, -1 }; mu_layout_row(&ctx, 2, widths, 50); + return args.Env().Undefined(); } -void MuEndWindow(const v8::FunctionCallbackInfo &args) { +Napi::Value MuEndWindow(const Napi::CallbackInfo &args) { mu_end_window(&ctx); + return args.Env().Undefined(); } -void MuUpdateInput(const v8::FunctionCallbackInfo &args) { +Napi::Value MuUpdateInput(const Napi::CallbackInfo &args) { int x = GetMouseX(); int y = GetMouseY(); mu_input_mousemove(&ctx, x, y); @@ -66,13 +66,15 @@ void MuUpdateInput(const v8::FunctionCallbackInfo &args) { input[1] = '\0'; mu_input_text(&ctx, input); } + return args.Env().Undefined(); } -void MuBegin(const v8::FunctionCallbackInfo &args) { +Napi::Value MuBegin(const Napi::CallbackInfo &args) { mu_begin(&ctx); + return args.Env().Undefined(); } -void MuEnd(const v8::FunctionCallbackInfo &args) { +Napi::Value MuEnd(const Napi::CallbackInfo &args) { mu_end(&ctx); mu_Command *cmd = NULL; @@ -101,46 +103,48 @@ void MuEnd(const v8::FunctionCallbackInfo &args) { DrawText(cmd->text.str, cmd->text.pos.x, cmd->text.pos.y, FONT_SIZE, *(Color*)&cmd->text.color); } break; // case MU_COMMAND_ICON: printf("MU_COMMAND_ICON\n"); break; + } } -} + return args.Env().Undefined(); } -void InitWindowAdapter(const v8::FunctionCallbackInfo &args) { - auto isolate = args.GetIsolate(); - auto context = isolate->GetCurrentContext(); +Napi::Value InitWindowAdapter(const Napi::CallbackInfo &args) { + auto env = args.Env(); int width = 0; - if (args.Length() > 0) width = args[0]->Int32Value(context).FromJust(); + if (args.Length() > 0) width = args[0].ToNumber().Int32Value(); int height = 0; - if (args.Length() > 1) height = args[1]->Int32Value(context).FromJust(); + if (args.Length() > 1) height = args[1].ToNumber().Int32Value(); const char *title = "Hardcoded Title"; if (args.Length() > 2) { - title = strdup(*v8::String::Utf8Value(isolate, args[2]->ToString(context).ToLocalChecked())); // memory leak + title = strdup(args[2].ToString().Utf8Value().c_str()); // memory leak } InitWindow(width, height, title); + return env.Undefined(); } -void WindowShouldCloseAdapter(const v8::FunctionCallbackInfo &args) { - auto isolate = args.GetIsolate(); - args.GetReturnValue().Set(v8::Boolean::New(isolate, WindowShouldClose())); +Napi::Value WindowShouldCloseAdapter(const Napi::CallbackInfo &args) { + return Napi::Boolean::New(args.Env(), WindowShouldClose()); } -void BeginDrawingAdapter(const v8::FunctionCallbackInfo &args) { +Napi::Value BeginDrawingAdapter(const Napi::CallbackInfo &args) { BeginDrawing(); + return args.Env().Undefined(); } -void EndDrawingAdapter(const v8::FunctionCallbackInfo &args) { +Napi::Value EndDrawingAdapter(const Napi::CallbackInfo &args) { EndDrawing(); + return args.Env().Undefined(); } -void ClearBackgroundAdapter(const v8::FunctionCallbackInfo &args) { - auto isolate = args.GetIsolate(); - auto context = isolate->GetCurrentContext(); - auto color = args[0]->Uint32Value(context).FromJust(); +Napi::Value ClearBackgroundAdapter(const Napi::CallbackInfo &args) { + auto env = args.Env(); + auto color = args[0].ToNumber().Uint32Value(); ClearBackground(*(Color*)&color); + return env.Undefined(); } int text_width(mu_Font font, const char *str, int len) @@ -154,7 +158,9 @@ int text_height(mu_Font font) return FONT_SIZE; } -void Initialize(v8::Local exports) { +#define NODE_SET_METHOD(exports, name, func) exports.Set(Napi::String::New(env, name), Napi::Function::New(env, func)); + +Napi::Object Initialize(Napi::Env env, Napi::Object exports) { mu_init(&ctx); ctx.text_width = text_width; ctx.text_height = text_height; @@ -172,6 +178,8 @@ void Initialize(v8::Local exports) { NODE_SET_METHOD(exports, "mu_button", MuButton); NODE_SET_METHOD(exports, "mu_label", MuLabel); NODE_SET_METHOD(exports, "mu_input", MuInput); + + return exports; } -NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize) +NODE_API_MODULE(NODE_GYP_MODULE_NAME, Initialize) diff --git a/packages/muray/package.json b/packages/muray/package.json index 1494055..44c9a7c 100644 --- a/packages/muray/package.json +++ b/packages/muray/package.json @@ -18,5 +18,8 @@ "bugs": { "url": "https://github.com/tsoding/Murayact/issues" }, - "homepage": "https://github.com/tsoding/Murayact#readme" + "homepage": "https://github.com/tsoding/Murayact#readme", + "dependencies": { + "node-addon-api": "*" + } }