Repository navigation
Expand file tree
/
Copy pathbuffer_native.c
More file actions
195 lines (174 loc) · 6.62 KB
/
Copy pathbuffer_native.c
File metadata and controls
195 lines (174 loc) · 6.62 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
// --------------------------------------------------
// Project: ProX Programming Language (ProXPL)
// Author: ProgrammerKR
// Created: 2026-04-27
// Copyright © 2025. ProXentix India Pvt. Ltd. All rights reserved.
// --------------------------------------------------
/*
* ProXPL Standard Library - Buffer Module
* Binary buffer: alloc, read/write bytes, hex dump.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../include/common.h"
#include "../../include/vm.h"
#include "../../include/value.h"
#include "../../include/object.h"
extern VM vm;
// Internal buffer structure stored inside ObjForeign->library pointer
typedef struct {
uint8_t* data;
int size;
int capacity;
} ProxBuffer;
// Helper: create / resize
static ProxBuffer* buf_new(int capacity) {
ProxBuffer* b = (ProxBuffer*)malloc(sizeof(ProxBuffer));
if (!b) return NULL;
b->data = (uint8_t*)calloc(capacity, 1);
b->size = 0;
b->capacity = capacity;
return b;
}
#if 0
static void buf_free_cb(void* ptr) {
if (!ptr) return;
ProxBuffer* b = (ProxBuffer*)ptr;
free(b->data);
free(b);
}
#endif
static void defineModuleFn(ObjModule* module, const char* name, NativeFn fn) {
ObjString* nameObj = copyString(name, (int)strlen(name));
push(&vm, OBJ_VAL(nameObj));
push(&vm, OBJ_VAL(newNative(fn)));
tableSet(&module->exports, nameObj, peek(&vm, 0));
pop(&vm);
pop(&vm);
}
// buffer.alloc(size) -> Buffer
static Value native_buf_alloc(int argCount, Value* args) {
int sz = (argCount >= 1 && IS_NUMBER(args[0])) ? (int)AS_NUMBER(args[0]) : 64;
if (sz <= 0) sz = 64;
ProxBuffer* b = buf_new(sz);
if (!b) return NIL_VAL;
ObjString* tag = copyString("Buffer", 6);
push(&vm, OBJ_VAL(tag));
ObjForeign* f = newForeign(tag, (void*)b, NULL);
pop(&vm);
return OBJ_VAL(f);
}
// buffer.write_byte(buf, byte) -> nil
static Value native_buf_write_byte(int argCount, Value* args) {
if (argCount < 2 || !IS_FOREIGN(args[0]) || !IS_NUMBER(args[1])) return NIL_VAL;
ProxBuffer* b = (ProxBuffer*)AS_FOREIGN(args[0])->library;
uint8_t byte = (uint8_t)((int)AS_NUMBER(args[1]) & 0xFF);
if (b->size >= b->capacity) {
int newCap = b->capacity * 2;
uint8_t* newData = (uint8_t*)realloc(b->data, newCap);
if (!newData) return NIL_VAL;
b->data = newData;
b->capacity = newCap;
}
b->data[b->size++] = byte;
return NIL_VAL;
}
// buffer.read_byte(buf, index) -> number
static Value native_buf_read_byte(int argCount, Value* args) {
if (argCount < 2 || !IS_FOREIGN(args[0]) || !IS_NUMBER(args[1])) return NUMBER_VAL(-1);
ProxBuffer* b = (ProxBuffer*)AS_FOREIGN(args[0])->library;
int idx = (int)AS_NUMBER(args[1]);
if (idx < 0 || idx >= b->size) return NUMBER_VAL(-1);
return NUMBER_VAL((double)b->data[idx]);
}
// buffer.size(buf) -> number
static Value native_buf_size(int argCount, Value* args) {
if (argCount < 1 || !IS_FOREIGN(args[0])) return NUMBER_VAL(0);
ProxBuffer* b = (ProxBuffer*)AS_FOREIGN(args[0])->library;
return NUMBER_VAL((double)b->size);
}
// buffer.write_string(buf, str) -> nil
static Value native_buf_write_str(int argCount, Value* args) {
if (argCount < 2 || !IS_FOREIGN(args[0]) || !IS_STRING(args[1])) return NIL_VAL;
ProxBuffer* b = (ProxBuffer*)AS_FOREIGN(args[0])->library;
ObjString* s = AS_STRING(args[1]);
int needed = b->size + s->length;
int newCap = b->capacity;
while (newCap < needed) {
newCap *= 2;
}
if (newCap > b->capacity) {
uint8_t* newData = (uint8_t*)realloc(b->data, newCap);
if (!newData) return NIL_VAL;
b->data = newData;
b->capacity = newCap;
}
memcpy(b->data + b->size, s->chars, s->length);
b->size += s->length;
return NIL_VAL;
}
// buffer.to_string(buf) -> string
static Value native_buf_to_string(int argCount, Value* args) {
if (argCount < 1 || !IS_FOREIGN(args[0])) return OBJ_VAL(copyString("", 0));
ProxBuffer* b = (ProxBuffer*)AS_FOREIGN(args[0])->library;
return OBJ_VAL(copyString((const char*)b->data, b->size));
}
// buffer.hex_dump(buf) -> string (like "48 65 6c 6c 6f")
static Value native_buf_hex_dump(int argCount, Value* args) {
if (argCount < 1 || !IS_FOREIGN(args[0])) return OBJ_VAL(copyString("", 0));
ProxBuffer* b = (ProxBuffer*)AS_FOREIGN(args[0])->library;
if (b->size == 0) return OBJ_VAL(copyString("", 0));
// Each byte: "XX " = 3 chars, last has no space -> size * 3 - 1
int outLen = b->size * 3 - 1;
char* out = (char*)malloc(outLen + 1);
if (!out) return OBJ_VAL(copyString("", 0));
for (int i = 0; i < b->size; i++) {
sprintf(out + i * 3, "%02x", b->data[i]);
if (i < b->size - 1) out[i * 3 + 2] = ' ';
}
out[outLen] = '\0';
Value result = OBJ_VAL(copyString(out, outLen));
free(out);
return result;
}
// buffer.clear(buf) -> nil
static Value native_buf_clear(int argCount, Value* args) {
if (argCount < 1 || !IS_FOREIGN(args[0])) return NIL_VAL;
ProxBuffer* b = (ProxBuffer*)AS_FOREIGN(args[0])->library;
memset(b->data, 0, b->capacity);
b->size = 0;
return NIL_VAL;
}
// buffer.slice(buf, start, length) -> string
static Value native_buf_slice(int argCount, Value* args) {
if (argCount < 3 || !IS_FOREIGN(args[0]) || !IS_NUMBER(args[1]) || !IS_NUMBER(args[2])) {
return OBJ_VAL(copyString("", 0));
}
ProxBuffer* b = (ProxBuffer*)AS_FOREIGN(args[0])->library;
int start = (int)AS_NUMBER(args[1]);
int length = (int)AS_NUMBER(args[2]);
if (start < 0) start = 0;
if (start >= b->size) return OBJ_VAL(copyString("", 0));
if (start + length > b->size) length = b->size - start;
if (length <= 0) return OBJ_VAL(copyString("", 0));
return OBJ_VAL(copyString((const char*)(b->data + start), length));
}
ObjModule* create_std_buffer_module() {
ObjString* name = copyString("std.native.buffer", 17);
push(&vm, OBJ_VAL(name));
ObjModule* module = newModule(name);
push(&vm, OBJ_VAL(module));
defineModuleFn(module, "alloc", native_buf_alloc);
defineModuleFn(module, "write_byte", native_buf_write_byte);
defineModuleFn(module, "read_byte", native_buf_read_byte);
defineModuleFn(module, "size", native_buf_size);
defineModuleFn(module, "write_string", native_buf_write_str);
defineModuleFn(module, "to_string", native_buf_to_string);
defineModuleFn(module, "hex_dump", native_buf_hex_dump);
defineModuleFn(module, "clear", native_buf_clear);
defineModuleFn(module, "slice", native_buf_slice);
pop(&vm); // module
pop(&vm); // name
return module;
}