|
1 | | -/* proxpl_api.c - public API wrappers for stable ABI |
2 | | - * Exposes a minimal C-style API that callers can use to embed runtime. |
3 | | - */ |
| 1 | +/* proxpl_api.c - public API wrappers for stable ABI */ |
4 | 2 |
|
5 | | -#include "proxpl_api.h" |
6 | | -#include "vm.h" |
7 | | -#include "chunk.h" |
| 3 | +#include <stdio.h> |
8 | 4 | #include <stdlib.h> |
| 5 | +#include <string.h> |
| 6 | + |
| 7 | +#include "../include/proxpl_api.h" |
| 8 | +#include "../include/vm.h" |
| 9 | +#include "../include/chunk.h" |
| 10 | +#include "../include/common.h" |
| 11 | + |
| 12 | +// Helper to read a file into a string |
| 13 | +static char* readFile(const char* path) { |
| 14 | + FILE* file = fopen(path, "rb"); |
| 15 | + if (file == NULL) { |
| 16 | + fprintf(stderr, "Could not open file \"%s\".\n", path); |
| 17 | + return NULL; |
| 18 | + } |
| 19 | + |
| 20 | + fseek(file, 0L, SEEK_END); |
| 21 | + size_t fileSize = ftell(file); |
| 22 | + rewind(file); |
| 23 | + |
| 24 | + char* buffer = (char*)malloc(fileSize + 1); |
| 25 | + if (buffer == NULL) { |
| 26 | + fprintf(stderr, "Not enough memory to read \"%s\".\n", path); |
| 27 | + fclose(file); |
| 28 | + return NULL; |
| 29 | + } |
| 30 | + |
| 31 | + size_t bytesRead = fread(buffer, sizeof(char), fileSize, file); |
| 32 | + if (bytesRead < fileSize) { |
| 33 | + fprintf(stderr, "Could not read file \"%s\".\n", path); |
| 34 | + free(buffer); |
| 35 | + fclose(file); |
| 36 | + return NULL; |
| 37 | + } |
| 38 | + |
| 39 | + buffer[bytesRead] = '\0'; |
| 40 | + fclose(file); |
| 41 | + return buffer; |
| 42 | +} |
9 | 43 |
|
10 | 44 | void proxpl_vm_init(VM *vm) { |
11 | | - initVM(vm); |
| 45 | + // Standard CLOX uses a global VM instance, so we ignore the pointer for now |
| 46 | + // or assume 'vm' points to the global one. |
| 47 | + initVM(); |
12 | 48 | } |
13 | 49 |
|
14 | 50 | void proxpl_vm_free(VM *vm) { |
15 | | - freeVM(vm); |
| 51 | + freeVM(); |
16 | 52 | } |
17 | 53 |
|
18 | 54 | InterpretResult proxpl_interpret_chunk(VM *vm, const Chunk *chunk) { |
19 | | - // interpret expects a mutable chunk; cast away const as the API currently |
20 | | - return interpret(vm, (Chunk *)chunk); |
| 55 | + // ERROR: The standard VM interprets Source Code strings, not raw Chunks. |
| 56 | + // To support this, you would need to expose the internal run() function. |
| 57 | + fprintf(stderr, "API Error: interpreting raw chunks is not supported in this version.\n"); |
| 58 | + return INTERPRET_RUNTIME_ERROR; |
21 | 59 | } |
22 | 60 |
|
23 | 61 | InterpretResult proxpl_interpret_file(VM *vm, const char *path) { |
24 | | - // Helper: read chunk and interpret |
25 | | - Chunk c; |
26 | | - if (read_chunk_from_file(path, &c) != 0) return INTERPRET_COMPILE_ERROR; |
27 | | - InterpretResult r = interpret(vm, &c); |
28 | | - chunk_free(&c); |
29 | | - return r; |
| 62 | + char* source = readFile(path); |
| 63 | + if (source == NULL) return INTERPRET_COMPILE_ERROR; |
| 64 | + |
| 65 | + // Pass the source string to interpret |
| 66 | + InterpretResult result = interpret(source); |
| 67 | + |
| 68 | + free(source); |
| 69 | + return result; |
30 | 70 | } |
31 | 71 |
|
| 72 | +// These functions are STUBBED because bytecode.c was deleted. |
| 73 | +// You must re-implement binary serialization later if you need it. |
32 | 74 | int proxpl_write_chunk_to_file(const char *path, const Chunk *chunk) { |
33 | | - return write_chunk_to_file(path, chunk); |
| 75 | + fprintf(stderr, "Feature 'write_chunk_to_file' is temporarily disabled.\n"); |
| 76 | + return -1; |
34 | 77 | } |
35 | 78 |
|
36 | 79 | int proxpl_read_chunk_from_file(const char *path, Chunk *out) { |
37 | | - return read_chunk_from_file(path, out); |
| 80 | + fprintf(stderr, "Feature 'read_chunk_from_file' is temporarily disabled.\n"); |
| 81 | + return -1; |
38 | 82 | } |
| 83 | + |
0 commit comments