Skip to content
Merged
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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ src/**/*.js
c_bridges/*.o

# Generated files
src/codegen/stdlib/embedded-dts.ts
lib/*.js
lib/*.js.map
lib/*.d.ts
Expand Down
52 changes: 19 additions & 33 deletions docs/.vitepress/theme/ExampleTabs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ const examples = [
{
label: 'HTTP Server',
file: 'server.ts',
code: `function handleRequest(req: HttpRequest): HttpResponse {
if (req.path == "/") {
return { status: 200, body: "Hello, world!" };
}
return { status: 404, body: "Not Found" };
code: `ChadScript.embedDir("./public");

function handleRequest(req: HttpRequest): HttpResponse {
return ChadScript.serveEmbedded(req.path);
}

httpServe(3000, handleRequest);`,
run: '$ chad run server.ts',
run: '$ chad build server.ts -o server && ./server',
output: `listening on port 3000`,
},
{
Expand All @@ -53,36 +52,23 @@ sqlite.close(db);`,
},
{
label: 'Async',
file: 'parallel.ts',
code: `async function main() {
const a = fetch("https://api.example.com/users");
const b = fetch("https://api.example.com/posts");
const [users, posts] = await Promise.all([a, b]);
console.log("users: " + users.status);
console.log("posts: " + posts.status);
}

main();`,
run: '$ chad run parallel.ts',
output: `users: 200\nposts: 200`,
},
{
label: 'Single-Binary Webapp',
file: 'app.ts',
code: `// HTML & CSS are embedded into the binary at compile time
ChadScript.embedDir("./public");
file: 'stars.ts',
code: `interface Repo { stargazers_count: number }

function handleRequest(req: HttpRequest): HttpResponse {
if (req.path == "/")
return { status: 200, body: ChadScript.getEmbeddedFile("index.html") };
if (req.path == "/style.css")
return { status: 200, body: ChadScript.getEmbeddedFile("style.css") };
return { status: 404, body: "Not Found" };
async function main() {
const results = await Promise.all([
fetch("https://api.github.com/repos/cs01/ChadScript"),
fetch("https://api.github.com/repos/facebook/react"),
]);
const cs = JSON.parse<Repo>(results[0].text());
const react = JSON.parse<Repo>(results[1].text());
console.log("ChadScript: " + cs.stargazers_count + " stars");
console.log("React: " + react.stargazers_count + " stars");
}

httpServe(3000, handleRequest);`,
run: '$ chad build app.ts -o webapp && ./webapp',
output: `listening on port 3000`,
main();`,
run: '$ chad run stars.ts',
output: `ChadScript: mass stars\nReact: 235k stars`,
},
]

Expand Down
8 changes: 2 additions & 6 deletions examples/hackernews/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,6 @@ function handleRequest(req: HttpRequest): HttpResponse {
return { status: 200, body: body };
}

if (req.method === "GET" && req.path === "/style.css") {
const css = ChadScript.getEmbeddedFile("style.css");
return { status: 200, body: css };
}

if (req.method === "POST" && req.path.startsWith("/upvote/")) {
const idStr = req.path.substring(8, req.path.length);
sqlite.exec(db, "UPDATE posts SET points = points + 1 WHERE id = ?", [idStr]);
Expand All @@ -140,7 +135,8 @@ function handleRequest(req: HttpRequest): HttpResponse {
return { status: 200, body: redirectHtml };
}

return { status: 404, body: "Not Found" };
// Serve all other embedded files (CSS, images, etc.) with a single line
return ChadScript.serveEmbedded(req.path);
}

console.log("Hacker News Clone");
Expand Down
21 changes: 12 additions & 9 deletions examples/parallel.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
// Parallel HTTP Fetches - demonstrates async/await with Promise.all

console.log("Parallel Fetch Demo");
console.log(" fetching two URLs concurrently with Promise.all...");
console.log("");
interface Repo {
stargazers_count: number;
}

async function main(): Promise<void> {
const a = fetch("https://api.example.com/users");
const b = fetch("https://api.example.com/posts");
const [users, posts] = await Promise.all([a, b]);
const results = await Promise.all([
fetch("https://api.github.com/repos/cs01/ChadScript"),
fetch("https://api.github.com/repos/facebook/react"),
]);

const cs = JSON.parse<Repo>(results[0].text());
const react = JSON.parse<Repo>(results[1].text());

console.log("Results:");
console.log(" https://api.example.com/users -> " + users.status);
console.log(" https://api.example.com/posts -> " + posts.status);
console.log("ChadScript: " + cs.stargazers_count + " stars");
console.log("React: " + react.stargazers_count + " stars");
}

main();
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
},
"scripts": {
"compile": "tsx src/chad-node.ts build",
"prebuild": "node scripts/embed-dts.js",
"build": "tsc",
"test": "node scripts/test.js",
"test:fast": "npm run build && node --import tsx --test tests/smoke.test.ts",
Expand Down
16 changes: 0 additions & 16 deletions scripts/embed-dts.js

This file was deleted.

4 changes: 2 additions & 2 deletions src/chad-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
addLinkLib,
addLinkPath,
} from "./native-compiler-lib.js";
import { getDtsContent } from "./codegen/stdlib/embedded-dts.js";
// d.ts content is embedded at compile time via ChadScript.embedFile
const dtsContent = ChadScript.embedFile("../chadscript.d.ts");
import { ArgumentParser } from "./argparse.js";

declare const fs: {
Expand Down Expand Up @@ -97,7 +98,6 @@ if (parser.getFlag("version")) {
const command = parser.getSubcommand();

if (command === "init") {
const dtsContent = getDtsContent();
if (fs.existsSync("chadscript.d.ts")) {
console.log(" skip chadscript.d.ts (already exists)");
} else {
Expand Down
13 changes: 10 additions & 3 deletions src/codegen/expressions/method-calls/string-methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,19 @@ export function handleStringArrayIndexOf(
): string {
const arrayPtr = ctx.generateExpression(expr.object, params);

if (expr.args.length !== 1) {
return ctx.emitError(`indexOf() expects 1 argument, got ${expr.args.length}`, expr.loc);
if (expr.args.length < 1 || expr.args.length > 2) {
return ctx.emitError(`indexOf() expects 1 or 2 arguments, got ${expr.args.length}`, expr.loc);
}

const searchValue = ctx.generateExpression(expr.args[0], params);

// Optional fromIndex (2nd arg) — defaults to 0
let fromIndex = "0";
if (expr.args.length === 2) {
const fromRaw = ctx.generateExpression(expr.args[1], params);
fromIndex = convertToI32(ctx, fromRaw);
}

const checkLabel = ctx.nextLabel("indexof_check");
const bodyLabel = ctx.nextLabel("indexof_body");
const foundLabel = ctx.nextLabel("indexof_found");
Expand Down Expand Up @@ -351,7 +358,7 @@ export function handleStringArrayIndexOf(
ctx.emitLabel(`${checkLabel}_start`);
const counterPtr = ctx.nextTemp();
ctx.emit(`${counterPtr} = alloca i32`);
ctx.emitStore("i32", "0", counterPtr);
ctx.emitStore("i32", fromIndex, counterPtr);

ctx.emitBr(checkLabel);

Expand Down
3 changes: 0 additions & 3 deletions src/codegen/stdlib/embedded-dts.ts

This file was deleted.

10 changes: 9 additions & 1 deletion src/codegen/stdlib/init-templates.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import * as fs from "fs";
import { getDtsContent } from "./embedded-dts.js";
import * as path from "path";
import { fileURLToPath } from "url";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

function getDtsContent(): string {
// Read the canonical chadscript.d.ts from the repo root
return fs.readFileSync(path.join(__dirname, "../../../chadscript.d.ts"), "utf8");
}

const TSCONFIG_CONTENT = `{
"compilerOptions": {
Expand Down
Loading
Loading