-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinternal_modules.c
More file actions
411 lines (350 loc) · 12 KB
/
internal_modules.c
File metadata and controls
411 lines (350 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// internal_modules.c - Creating importable native modules
//
// Demonstrates:
// - Creating internal modules with native functions
// - Adding value exports (constants)
// - Multiple modules with different specifiers
// - Importing and using native modules from JavaScript
//
// This shows how to create Node.js-style native modules that can be
// imported using ES module syntax: import { add } from "myapp:math";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "tsrun.h"
#include "tsrun_console.h"
// ============================================================================
// Math module functions
// ============================================================================
static TsRunValue* math_add(
TsRunContext* ctx,
TsRunValue* this_arg,
TsRunValue** args,
size_t argc,
void* userdata,
const char** error_out
) {
(void)this_arg;
(void)userdata;
double a = (argc > 0 && tsrun_is_number(args[0])) ? tsrun_get_number(args[0]) : 0.0;
double b = (argc > 1 && tsrun_is_number(args[1])) ? tsrun_get_number(args[1]) : 0.0;
return tsrun_number(ctx, a + b);
}
static TsRunValue* math_multiply(
TsRunContext* ctx,
TsRunValue* this_arg,
TsRunValue** args,
size_t argc,
void* userdata,
const char** error_out
) {
(void)this_arg;
(void)userdata;
double a = (argc > 0 && tsrun_is_number(args[0])) ? tsrun_get_number(args[0]) : 0.0;
double b = (argc > 1 && tsrun_is_number(args[1])) ? tsrun_get_number(args[1]) : 1.0;
return tsrun_number(ctx, a * b);
}
static TsRunValue* math_pow(
TsRunContext* ctx,
TsRunValue* this_arg,
TsRunValue** args,
size_t argc,
void* userdata,
const char** error_out
) {
(void)this_arg;
(void)userdata;
if (argc < 2) {
*error_out = "pow() requires 2 arguments";
return NULL;
}
double base = tsrun_is_number(args[0]) ? tsrun_get_number(args[0]) : 0.0;
double exp = tsrun_is_number(args[1]) ? tsrun_get_number(args[1]) : 0.0;
double result = 1.0;
for (int i = 0; i < (int)exp; i++) {
result *= base;
}
return tsrun_number(ctx, result);
}
// ============================================================================
// String module functions
// ============================================================================
static TsRunValue* string_uppercase(
TsRunContext* ctx,
TsRunValue* this_arg,
TsRunValue** args,
size_t argc,
void* userdata,
const char** error_out
) {
(void)this_arg;
(void)userdata;
if (argc < 1 || !tsrun_is_string(args[0])) {
*error_out = "uppercase() requires a string argument";
return NULL;
}
const char* input = tsrun_get_string(args[0]);
size_t len = strlen(input);
char* result = malloc(len + 1);
if (!result) {
*error_out = "Memory allocation failed";
return NULL;
}
for (size_t i = 0; i < len; i++) {
result[i] = (char)toupper((unsigned char)input[i]);
}
result[len] = '\0';
TsRunValue* ret = tsrun_string(ctx, result);
free(result);
return ret;
}
static TsRunValue* string_reverse(
TsRunContext* ctx,
TsRunValue* this_arg,
TsRunValue** args,
size_t argc,
void* userdata,
const char** error_out
) {
(void)this_arg;
(void)userdata;
if (argc < 1 || !tsrun_is_string(args[0])) {
*error_out = "reverse() requires a string argument";
return NULL;
}
const char* input = tsrun_get_string(args[0]);
size_t len = strlen(input);
char* result = malloc(len + 1);
if (!result) {
*error_out = "Memory allocation failed";
return NULL;
}
for (size_t i = 0; i < len; i++) {
result[i] = input[len - 1 - i];
}
result[len] = '\0';
TsRunValue* ret = tsrun_string(ctx, result);
free(result);
return ret;
}
static TsRunValue* string_repeat(
TsRunContext* ctx,
TsRunValue* this_arg,
TsRunValue** args,
size_t argc,
void* userdata,
const char** error_out
) {
(void)this_arg;
(void)userdata;
if (argc < 2) {
*error_out = "repeat() requires 2 arguments (string, count)";
return NULL;
}
if (!tsrun_is_string(args[0]) || !tsrun_is_number(args[1])) {
*error_out = "repeat() requires (string, number) arguments";
return NULL;
}
const char* input = tsrun_get_string(args[0]);
int count = (int)tsrun_get_number(args[1]);
size_t len = strlen(input);
if (count <= 0) {
return tsrun_string(ctx, "");
}
size_t result_len = len * count;
char* result = malloc(result_len + 1);
if (!result) {
*error_out = "Memory allocation failed";
return NULL;
}
for (int i = 0; i < count; i++) {
memcpy(result + i * len, input, len);
}
result[result_len] = '\0';
TsRunValue* ret = tsrun_string(ctx, result);
free(result);
return ret;
}
// ============================================================================
// Module setup
// ============================================================================
static void setup_modules(TsRunContext* ctx) {
// Create "myapp:math" module
TsRunInternalModule* math_mod = tsrun_internal_module_new("myapp:math");
// Add function exports
tsrun_internal_module_add_function(math_mod, "add", math_add, 2, NULL);
tsrun_internal_module_add_function(math_mod, "multiply", math_multiply, 2, NULL);
tsrun_internal_module_add_function(math_mod, "pow", math_pow, 2, NULL);
// Add value exports (constants)
tsrun_internal_module_add_value(math_mod, "PI", tsrun_number(ctx, 3.14159265358979));
tsrun_internal_module_add_value(math_mod, "E", tsrun_number(ctx, 2.71828182845905));
// Register the module
TsRunResult result = tsrun_register_internal_module(ctx, math_mod);
if (!result.ok) {
printf("Failed to register myapp:math module: %s\n", result.error);
}
// Create "myapp:string" module
TsRunInternalModule* string_mod = tsrun_internal_module_new("myapp:string");
tsrun_internal_module_add_function(string_mod, "uppercase", string_uppercase, 1, NULL);
tsrun_internal_module_add_function(string_mod, "reverse", string_reverse, 1, NULL);
tsrun_internal_module_add_function(string_mod, "repeat", string_repeat, 2, NULL);
// Add string constants
tsrun_internal_module_add_value(string_mod, "EMPTY", tsrun_string(ctx, ""));
tsrun_internal_module_add_value(string_mod, "NEWLINE", tsrun_string(ctx, "\n"));
result = tsrun_register_internal_module(ctx, string_mod);
if (!result.ok) {
printf("Failed to register myapp:string module: %s\n", result.error);
}
}
// ============================================================================
// Helper to run code with module support
// ============================================================================
static void run_code(const char* title, const char* code) {
printf("\n========================================\n");
printf("%s\n", title);
printf("========================================\n");
printf("\nCode:\n%s\n", code);
printf("\n--- Output ---\n");
TsRunContext* ctx = tsrun_new();
tsrun_set_console(ctx, tsrun_console_stdio, NULL);
// Set up internal modules before running code
setup_modules(ctx);
// Prepare and run
TsRunResult prep = tsrun_prepare(ctx, code, "/main.ts");
if (!prep.ok) {
printf("Prepare error: %s\n", prep.error);
tsrun_free(ctx);
return;
}
TsRunStepResult result;
tsrun_run(&result, ctx);
if (result.status == TSRUN_STEP_COMPLETE) {
printf("\n--- Result ---\n");
if (result.value) {
if (tsrun_is_string(result.value)) {
printf("%s\n", tsrun_get_string(result.value));
} else if (tsrun_is_number(result.value)) {
printf("%g\n", tsrun_get_number(result.value));
} else {
char* json = tsrun_json_stringify(ctx, result.value);
if (json) {
printf("%s\n", json);
tsrun_free_string(json);
}
}
} else {
printf("undefined\n");
}
} else if (result.status == TSRUN_STEP_ERROR) {
printf("\n--- Error ---\n%s\n", result.error);
}
tsrun_step_result_free(&result);
tsrun_free(ctx);
}
// ============================================================================
// Examples
// ============================================================================
static void example_basic_import(void) {
run_code(
"Example 1: Basic module import",
"import { add, multiply, PI } from 'myapp:math';\n"
"\n"
"const sum = add(10, 20);\n"
"const product = multiply(5, 6);\n"
"const circumference = multiply(2, multiply(PI, 5));\n"
"\n"
"console.log('Sum:', sum);\n"
"console.log('Product:', product);\n"
"console.log('Circumference (r=5):', circumference);\n"
"\n"
"({ sum, product, circumference });\n"
);
}
static void example_string_module(void) {
run_code(
"Example 2: String module functions",
"import { uppercase, reverse, repeat } from 'myapp:string';\n"
"\n"
"const text = 'hello world';\n"
"\n"
"console.log('Original:', text);\n"
"console.log('Uppercase:', uppercase(text));\n"
"console.log('Reversed:', reverse(text));\n"
"console.log('Repeated 3x:', repeat(text, 3));\n"
"\n"
"uppercase(text);\n"
);
}
static void example_namespace_import(void) {
run_code(
"Example 3: Namespace import",
"import * as math from 'myapp:math';\n"
"import * as str from 'myapp:string';\n"
"\n"
"// Use qualified names\n"
"const result = math.add(math.PI, math.E);\n"
"console.log('PI + E =', result);\n"
"\n"
"const greeting = str.uppercase('hello');\n"
"console.log('Greeting:', greeting);\n"
"\n"
"result;\n"
);
}
static void example_combined_usage(void) {
run_code(
"Example 4: Combined module usage",
"import { pow, PI } from 'myapp:math';\n"
"import { uppercase, reverse } from 'myapp:string';\n"
"\n"
"// Calculate area of circle with radius 10\n"
"const radius = 10;\n"
"const area = PI * pow(radius, 2);\n"
"console.log(`Area of circle (r=${radius}):`, area);\n"
"\n"
"// Play with strings\n"
"const name = 'typescript';\n"
"const processed = uppercase(reverse(name));\n"
"console.log('Processed name:', processed);\n"
"\n"
"({ area, processed });\n"
);
}
static void example_constants(void) {
run_code(
"Example 5: Using exported constants",
"import { PI, E } from 'myapp:math';\n"
"import { EMPTY, NEWLINE } from 'myapp:string';\n"
"\n"
"console.log('Math constants:');\n"
"console.log(' PI =', PI);\n"
"console.log(' E =', E);\n"
"\n"
"console.log('String constants:');\n"
"console.log(' EMPTY is empty:', EMPTY === '');\n"
"console.log(' NEWLINE:', JSON.stringify(NEWLINE));\n"
"\n"
"({ PI, E });\n"
);
}
// ============================================================================
// Main
// ============================================================================
int main(void) {
printf("tsrun C API - Internal Modules Example\n");
printf("======================================\n\n");
printf("This example demonstrates how to create native modules\n");
printf("that can be imported from JavaScript using ES module syntax.\n");
printf("\n");
printf("Two modules are created:\n");
printf(" - 'myapp:math' - Math functions and constants\n");
printf(" - 'myapp:string' - String manipulation functions\n");
example_basic_import();
example_string_module();
example_namespace_import();
example_combined_usage();
example_constants();
printf("\nDone!\n");
return 0;
}