-
Notifications
You must be signed in to change notification settings - Fork 7
Move existing generators to a separate file #328
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import type { FunctionDecl } from "../../src/node-api-functions.js"; | ||
|
|
||
| type FunctionOptions = FunctionDecl & { | ||
| extern?: true; | ||
| static?: true; | ||
| namespace?: string; | ||
| body?: string; | ||
| argumentNames?: string[]; | ||
| }; | ||
|
|
||
| export function generateFunction({ | ||
| extern, | ||
| static: staticMember, | ||
| returnType, | ||
| namespace, | ||
| name, | ||
| argumentTypes, | ||
| argumentNames = [], | ||
| noReturn, | ||
| body, | ||
| }: FunctionOptions) { | ||
| return ` | ||
| ${staticMember ? "static " : ""}${extern ? 'extern "C" ' : ""}${returnType} ${namespace ? namespace + "::" : ""}${name}( | ||
| ${argumentTypes.map((type, index) => `${type} ` + (argumentNames[index] ?? `arg${index}`)).join(", ")} | ||
| ) ${body ? `{ ${body} ${noReturn ? "WEAK_NODE_API_UNREACHABLE;" : ""}\n}` : ""} | ||
| ; | ||
| `; | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,74 @@ | ||||||||||||||||||||||||
| import type { FunctionDecl } from "../../src/node-api-functions.js"; | ||||||||||||||||||||||||
| import { generateFunction } from "./shared.js"; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| export function generateFunctionDecl({ | ||||||||||||||||||||||||
| returnType, | ||||||||||||||||||||||||
| name, | ||||||||||||||||||||||||
| argumentTypes, | ||||||||||||||||||||||||
| }: FunctionDecl) { | ||||||||||||||||||||||||
| return `${returnType} (*${name})(${argumentTypes.join(", ")});`; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * Generates source code for a version script for the given Node API version. | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
| export function generateHeader(functions: FunctionDecl[]) { | ||||||||||||||||||||||||
| return ` | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
| return ` | |
| return ` | |
| /** | |
| * @file weak_node_api.hpp | |
| * @brief Weak Node-API host injection interface. | |
| * | |
| * This header provides the struct and injection function for | |
| * dynamically supplying Node-API function pointers to the host. | |
| * | |
| * #pragma once is used as an include guard to prevent multiple inclusion. | |
| */ |
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'll be adding this in a followup PR.
Copilot
AI
Nov 10, 2025
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.
[nitpick] Corrected comment: 'Node-API' should be 'Node-API headers' for consistency with other include comments.
| #include <node_api.h> // Node-API | |
| #include <node_api.h> // Node-API headers |
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.
The error message will display 'undefined' when
stderris empty. ThespawnSyncoptions specifyencoding: 'utf8', but when status is non-zero and stderr is empty, this will show an unhelpful message. Consider usingstderr || 'no error output'or checking if stderr exists before interpolating it.