-
Notifications
You must be signed in to change notification settings - Fork 12
feat: Add C++ language support #40
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
base: main
Are you sure you want to change the base?
Changes from all commits
8916d42
1de4cdb
7e286b7
372633c
dbd3559
5baa078
3265c9f
fe68688
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 | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,21 @@ | ||||||||||||||||||
| FROM docker.io/cloudflare/sandbox:0.6.0-python | ||||||||||||||||||
|
|
||||||||||||||||||
| # Install C++ compiler and tools for C++ language support | ||||||||||||||||||
| RUN apt-get update && apt-get install -y \ | ||||||||||||||||||
| g++ \ | ||||||||||||||||||
| build-essential \ | ||||||||||||||||||
| git \ | ||||||||||||||||||
| && rm -rf /var/lib/apt/lists/* | ||||||||||||||||||
|
|
||||||||||||||||||
| # Install nlohmann/json library (header-only JSON library for C++) | ||||||||||||||||||
| RUN mkdir -p /usr/local/include && \ | ||||||||||||||||||
| cd /tmp && \ | ||||||||||||||||||
| git clone --depth 1 https://github.com/nlohmann/json.git && \ | ||||||||||||||||||
| cp json/single_include/nlohmann/json.hpp /usr/local/include/ && \ | ||||||||||||||||||
|
Comment on lines
+11
to
+14
|
||||||||||||||||||
| RUN mkdir -p /usr/local/include && \ | |
| cd /tmp && \ | |
| git clone --depth 1 https://github.com/nlohmann/json.git && \ | |
| cp json/single_include/nlohmann/json.hpp /usr/local/include/ && \ | |
| RUN mkdir -p /usr/local/include/nlohmann && \ | |
| cd /tmp && \ | |
| git clone --depth 1 https://github.com/nlohmann/json.git && \ | |
| cp json/single_include/nlohmann/json.hpp /usr/local/include/nlohmann/ && \ |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,186 @@ | ||||||||||||||
| import type { FunctionSignatureSchema, TypeDef } from "@repo/api-types"; | ||||||||||||||
| import type { CodeGenerator } from "./types"; | ||||||||||||||
|
|
||||||||||||||
| export class CppGenerator implements CodeGenerator { | ||||||||||||||
| private includes = new Set<string>(); | ||||||||||||||
|
|
||||||||||||||
| typeToString(typeDef: TypeDef): string { | ||||||||||||||
| switch (typeDef.kind) { | ||||||||||||||
| case "primitive": { | ||||||||||||||
| const map: Record<string, string> = { | ||||||||||||||
| int: "long long", | ||||||||||||||
| float: "double", | ||||||||||||||
| string: "std::string", | ||||||||||||||
| boolean: "bool", | ||||||||||||||
| null: "std::nullptr_t", | ||||||||||||||
| }; | ||||||||||||||
| return map[typeDef.type]; | ||||||||||||||
|
||||||||||||||
| return map[typeDef.type]; | |
| const mapped = map[typeDef.type]; | |
| if (!mapped) { | |
| throw new Error(`Unsupported primitive type: ${typeDef.type}`); | |
| } | |
| return mapped; |
Copilot
AI
Dec 7, 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.
The generateTypeDefinitions method generates constructors for struct definitions, but it doesn't handle the case where an object has no properties (empty properties object). This would result in invalid C++ syntax with trailing colons in the constructor. Consider adding a check:
if (nt.definition.kind === "object") {
const props = Object.entries(nt.definition.properties)
.map(([k, v]) => ` ${this.typeToString(v)} ${k};`)
.join("\n");
// Only generate constructor if there are properties
if (Object.keys(nt.definition.properties).length === 0) {
return `struct ${nt.name} {\n${props}\n};`;
}
const constructorParams = Object.entries(nt.definition.properties)
.map(([k, v]) => `${this.typeToString(v)} _${k}`)
.join(", ");
const constructorInits = Object.keys(nt.definition.properties)
.map((k) => `${k}(_${k})`)
.join(", ");
return `struct ${nt.name} {\n${props}\n ${nt.name}(${constructorParams}) : ${constructorInits} {}\n};`;
}
Copilot
AI
Dec 7, 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.
The C++ generator doesn't handle optional parameters (unlike TypeScript and Python generators which check p.optional). If the schema includes optional parameters, they won't be properly represented in C++. Consider adding support for optional parameters using std::optional<T>:
const params = schema.parameters
.map((p) => {
const typeStr = this.typeToString(p.type);
if (p.optional) {
this.includes.add("optional");
return `std::optional<${typeStr}> ${p.name}`;
}
return `${typeStr} ${p.name}`;
})
.join(", ");| const typeStr = this.typeToString(p.type); | |
| const typeStr = this.typeToString(p.type); | |
| if (p.optional) { | |
| this.includes.add("optional"); | |
| return `std::optional<${typeStr}> ${p.name}`; | |
| } |
Copilot
AI
Dec 7, 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] The generateRunnerCode method doesn't handle the case where a function has no parameters. When schema.parameters is empty, paramDeserializations will be an empty string and paramNames will be an empty string, but the generated code would still have comments and structure that suggests parameters. While this should still compile correctly, consider adding a comment or adjusting the code generation for clarity in the zero-parameter case.
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 nlohmann/json library is installed from the latest commit on the main branch without a version pin. This could lead to unexpected behavior if the library's API changes. Consider pinning to a specific tag/release for reproducibility.