mirror of
https://github.com/NeoFlock/neonucleus.git
synced 2025-09-24 09:03:32 +02:00
experimental baremetal
we no longer depend on libc
This commit is contained in:
parent
779d5a0c19
commit
51b3602088
34
build.zig
34
build.zig
@ -7,20 +7,15 @@ const LibBuildOpts = struct {
|
|||||||
baremetal: bool,
|
baremetal: bool,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn addEngineSources(b: *std.Build, c: *std.Build.Step.Compile, opts: LibBuildOpts) void {
|
fn addEngineSources(b: *std.Build, opts: LibBuildOpts) *std.Build.Module {
|
||||||
const dataMod = b.createModule(.{
|
const dataMod = b.createModule(.{
|
||||||
.root_source_file = b.path("src/data.zig"),
|
.root_source_file = b.path("src/data.zig"),
|
||||||
.target = opts.target,
|
.target = opts.target,
|
||||||
.optimize = opts.optimize,
|
.optimize = opts.optimize,
|
||||||
|
.single_threaded = true,
|
||||||
});
|
});
|
||||||
const zigObj = b.addObject(.{
|
|
||||||
.name = "zig_wrappers",
|
dataMod.addCSourceFiles(.{
|
||||||
.root_module = dataMod,
|
|
||||||
.pic = true,
|
|
||||||
});
|
|
||||||
c.addObject(zigObj);
|
|
||||||
|
|
||||||
c.addCSourceFiles(.{
|
|
||||||
.files = &[_][]const u8{
|
.files = &[_][]const u8{
|
||||||
"src/lock.c",
|
"src/lock.c",
|
||||||
"src/utils.c",
|
"src/utils.c",
|
||||||
@ -43,15 +38,17 @@ fn addEngineSources(b: *std.Build, c: *std.Build.Step.Compile, opts: LibBuildOpt
|
|||||||
});
|
});
|
||||||
|
|
||||||
if(!opts.baremetal) {
|
if(!opts.baremetal) {
|
||||||
c.linkLibC(); // we need a libc
|
dataMod.link_libc = true; // we need a libc
|
||||||
c.addCSourceFiles(.{
|
dataMod.addCSourceFiles(.{
|
||||||
.files = &.{
|
.files = &.{
|
||||||
"src/tinycthread.c",
|
"src/tinycthread.c",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
c.addIncludePath(b.path("src"));
|
dataMod.addIncludePath(b.path("src"));
|
||||||
|
|
||||||
|
return dataMod;
|
||||||
}
|
}
|
||||||
|
|
||||||
const LuaVersion = enum {
|
const LuaVersion = enum {
|
||||||
@ -114,24 +111,19 @@ pub fn build(b: *std.Build) void {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const includeFiles = b.addInstallHeaderFile(b.path("src/neonucleus.h"), "neonucleus.h");
|
const includeFiles = b.addInstallHeaderFile(b.path("src/neonucleus.h"), "neonucleus.h");
|
||||||
|
|
||||||
|
const engineMod = addEngineSources(b, opts);
|
||||||
|
|
||||||
const engineStatic = b.addStaticLibrary(.{
|
const engineStatic = b.addStaticLibrary(.{
|
||||||
.name = "neonucleus",
|
.name = "neonucleus",
|
||||||
//.root_source_file = b.path("src/engine.zig"),
|
.root_module = engineMod,
|
||||||
.target = target,
|
|
||||||
.optimize = optimize,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
addEngineSources(b, engineStatic, opts);
|
|
||||||
|
|
||||||
const engineShared = b.addSharedLibrary(.{
|
const engineShared = b.addSharedLibrary(.{
|
||||||
.name = getSharedEngineName(os),
|
.name = getSharedEngineName(os),
|
||||||
.target = target,
|
.root_module = engineMod,
|
||||||
.optimize = optimize,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
addEngineSources(b, engineShared, opts);
|
|
||||||
|
|
||||||
const engineStep = b.step("engine", "Builds the engine as a static library");
|
const engineStep = b.step("engine", "Builds the engine as a static library");
|
||||||
engineStep.dependOn(&engineStatic.step);
|
engineStep.dependOn(&engineStatic.step);
|
||||||
engineStep.dependOn(&includeFiles.step);
|
engineStep.dependOn(&includeFiles.step);
|
||||||
|
@ -22,7 +22,7 @@ nn_componentTable *nn_newComponentTable(nn_Alloc *alloc, const char *typeName, v
|
|||||||
void nn_destroyComponentTable(nn_componentTable *table) {
|
void nn_destroyComponentTable(nn_componentTable *table) {
|
||||||
nn_Alloc alloc = table->alloc;
|
nn_Alloc alloc = table->alloc;
|
||||||
nn_deallocStr(&alloc, table->name);
|
nn_deallocStr(&alloc, table->name);
|
||||||
for(size_t i = 0; i < table->methodCount; i++) {
|
for(nn_size_t i = 0; i < table->methodCount; i++) {
|
||||||
nn_method method = table->methods[i];
|
nn_method method = table->methods[i];
|
||||||
nn_deallocStr(&alloc, method.name);
|
nn_deallocStr(&alloc, method.name);
|
||||||
nn_deallocStr(&alloc, method.doc);
|
nn_deallocStr(&alloc, method.doc);
|
||||||
@ -47,7 +47,7 @@ void nn_defineMethod(nn_componentTable *table, const char *methodName, nn_bool_t
|
|||||||
table->methodCount++;
|
table->methodCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *nn_getTableMethod(nn_componentTable *table, size_t idx, nn_bool_t *outDirect) {
|
const char *nn_getTableMethod(nn_componentTable *table, nn_size_t idx, nn_bool_t *outDirect) {
|
||||||
if(idx >= table->methodCount) return NULL;
|
if(idx >= table->methodCount) return NULL;
|
||||||
nn_method method = table->methods[idx];
|
nn_method method = table->methods[idx];
|
||||||
if(outDirect != NULL) *outDirect = method.direct;
|
if(outDirect != NULL) *outDirect = method.direct;
|
||||||
@ -55,7 +55,7 @@ const char *nn_getTableMethod(nn_componentTable *table, size_t idx, nn_bool_t *o
|
|||||||
}
|
}
|
||||||
|
|
||||||
const char *nn_methodDoc(nn_componentTable *table, const char *methodName) {
|
const char *nn_methodDoc(nn_componentTable *table, const char *methodName) {
|
||||||
for(size_t i = 0; i < table->methodCount; i++) {
|
for(nn_size_t i = 0; i < table->methodCount; i++) {
|
||||||
if(nn_strcmp(table->methods[i].name, methodName) == 0) {
|
if(nn_strcmp(table->methods[i].name, methodName) == 0) {
|
||||||
return table->methods[i].doc;
|
return table->methods[i].doc;
|
||||||
}
|
}
|
||||||
@ -89,7 +89,7 @@ void *nn_getComponentUserdata(nn_component *component) {
|
|||||||
|
|
||||||
nn_bool_t nn_invokeComponentMethod(nn_component *component, const char *name) {
|
nn_bool_t nn_invokeComponentMethod(nn_component *component, const char *name) {
|
||||||
nn_componentTable *table = component->table;
|
nn_componentTable *table = component->table;
|
||||||
for(size_t i = 0; i < table->methodCount; i++) {
|
for(nn_size_t i = 0; i < table->methodCount; i++) {
|
||||||
nn_method method = table->methods[i];
|
nn_method method = table->methods[i];
|
||||||
if(nn_strcmp(method.name, name) == 0) {
|
if(nn_strcmp(method.name, name) == 0) {
|
||||||
nn_callCost(component->computer, NN_CALL_COST);
|
nn_callCost(component->computer, NN_CALL_COST);
|
||||||
|
@ -19,7 +19,7 @@ typedef struct nn_componentTable {
|
|||||||
nn_componentConstructor *constructor;
|
nn_componentConstructor *constructor;
|
||||||
nn_componentDestructor *destructor;
|
nn_componentDestructor *destructor;
|
||||||
nn_method methods[NN_MAX_METHODS];
|
nn_method methods[NN_MAX_METHODS];
|
||||||
size_t methodCount;
|
nn_size_t methodCount;
|
||||||
} nn_componentTable;
|
} nn_componentTable;
|
||||||
|
|
||||||
typedef struct nn_component {
|
typedef struct nn_component {
|
||||||
|
@ -14,7 +14,7 @@ nn_driveControl nn_drive_getControl(nn_component *component, nn_drive *drive) {
|
|||||||
|
|
||||||
void nn_drive_getLabel(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
|
void nn_drive_getLabel(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
|
||||||
char buf[NN_LABEL_SIZE];
|
char buf[NN_LABEL_SIZE];
|
||||||
size_t l = NN_LABEL_SIZE;
|
nn_size_t l = NN_LABEL_SIZE;
|
||||||
drive->getLabel(component, drive->userdata, buf, &l);
|
drive->getLabel(component, drive->userdata, buf, &l);
|
||||||
if(l == 0) {
|
if(l == 0) {
|
||||||
nn_return(computer, nn_values_nil());
|
nn_return(computer, nn_values_nil());
|
||||||
@ -23,7 +23,7 @@ void nn_drive_getLabel(nn_drive *drive, void *_, nn_component *component, nn_com
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
void nn_drive_setLabel(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
|
void nn_drive_setLabel(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
|
||||||
size_t l = 0;
|
nn_size_t l = 0;
|
||||||
nn_value label = nn_getArgument(computer, 0);
|
nn_value label = nn_getArgument(computer, 0);
|
||||||
const char *buf = nn_toString(label, &l);
|
const char *buf = nn_toString(label, &l);
|
||||||
if(buf == NULL) {
|
if(buf == NULL) {
|
||||||
@ -34,21 +34,21 @@ void nn_drive_setLabel(nn_drive *drive, void *_, nn_component *component, nn_com
|
|||||||
nn_return_string(computer, buf, l);
|
nn_return_string(computer, buf, l);
|
||||||
}
|
}
|
||||||
void nn_drive_getSectorSize(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
|
void nn_drive_getSectorSize(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
|
||||||
size_t sector_size = drive->getSectorSize(component, drive->userdata);
|
nn_size_t sector_size = drive->getSectorSize(component, drive->userdata);
|
||||||
nn_return(computer, nn_values_integer(sector_size));
|
nn_return(computer, nn_values_integer(sector_size));
|
||||||
}
|
}
|
||||||
void nn_drive_getPlatterCount(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
|
void nn_drive_getPlatterCount(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
|
||||||
size_t platter_count = drive->getPlatterCount(component, drive->userdata);
|
nn_size_t platter_count = drive->getPlatterCount(component, drive->userdata);
|
||||||
nn_return(computer, nn_values_integer(platter_count));
|
nn_return(computer, nn_values_integer(platter_count));
|
||||||
}
|
}
|
||||||
void nn_drive_getCapacity(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
|
void nn_drive_getCapacity(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
|
||||||
size_t capacity = drive->getCapacity(component, drive->userdata);
|
nn_size_t capacity = drive->getCapacity(component, drive->userdata);
|
||||||
nn_return(computer, nn_values_integer(capacity));
|
nn_return(computer, nn_values_integer(capacity));
|
||||||
}
|
}
|
||||||
void nn_drive_readSector(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
|
void nn_drive_readSector(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
|
||||||
nn_value sectorValue = nn_getArgument(computer, 0);
|
nn_value sectorValue = nn_getArgument(computer, 0);
|
||||||
int sector = nn_toInt(sectorValue);
|
int sector = nn_toInt(sectorValue);
|
||||||
size_t sector_size = drive->getSectorSize(component, drive->userdata);
|
nn_size_t sector_size = drive->getSectorSize(component, drive->userdata);
|
||||||
// we leave the +1 intentionally to compare the end of the real sector
|
// we leave the +1 intentionally to compare the end of the real sector
|
||||||
if (sector < 1 || (sector * sector_size > drive->getCapacity(component, drive->userdata))) {
|
if (sector < 1 || (sector * sector_size > drive->getCapacity(component, drive->userdata))) {
|
||||||
nn_setCError(computer, "bad argument #1 (sector out of range)");
|
nn_setCError(computer, "bad argument #1 (sector out of range)");
|
||||||
@ -61,10 +61,10 @@ void nn_drive_readSector(nn_drive *drive, void *_, nn_component *component, nn_c
|
|||||||
void nn_drive_writeSector(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
|
void nn_drive_writeSector(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
|
||||||
nn_value sectorValue = nn_getArgument(computer, 0);
|
nn_value sectorValue = nn_getArgument(computer, 0);
|
||||||
int sector = nn_toInt(sectorValue);
|
int sector = nn_toInt(sectorValue);
|
||||||
size_t sector_size = drive->getSectorSize(component, drive->userdata);
|
nn_size_t sector_size = drive->getSectorSize(component, drive->userdata);
|
||||||
nn_value bufValue = nn_getArgument(computer, 1);
|
nn_value bufValue = nn_getArgument(computer, 1);
|
||||||
|
|
||||||
size_t buf_size = 0;
|
nn_size_t buf_size = 0;
|
||||||
const char *buf = nn_toString(bufValue, &buf_size);
|
const char *buf = nn_toString(bufValue, &buf_size);
|
||||||
if (buf_size != sector_size) {
|
if (buf_size != sector_size) {
|
||||||
nn_setCError(computer, "bad argument #2 (expected buffer of length `sectorSize`)");
|
nn_setCError(computer, "bad argument #2 (expected buffer of length `sectorSize`)");
|
||||||
@ -79,10 +79,10 @@ void nn_drive_writeSector(nn_drive *drive, void *_, nn_component *component, nn_
|
|||||||
}
|
}
|
||||||
void nn_drive_readByte(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
|
void nn_drive_readByte(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
|
||||||
nn_value offsetValue = nn_getArgument(computer, 0);
|
nn_value offsetValue = nn_getArgument(computer, 0);
|
||||||
size_t disk_offset = nn_toInt(offsetValue) - 1;
|
nn_size_t disk_offset = nn_toInt(offsetValue) - 1;
|
||||||
size_t sector_size = drive->getSectorSize(component, drive->userdata);
|
nn_size_t sector_size = drive->getSectorSize(component, drive->userdata);
|
||||||
int sector = (disk_offset / sector_size) + 1;
|
int sector = (disk_offset / sector_size) + 1;
|
||||||
size_t sector_offset = disk_offset % sector_size;
|
nn_size_t sector_offset = disk_offset % sector_size;
|
||||||
|
|
||||||
if (disk_offset >= drive->getCapacity(component, drive->userdata)) {
|
if (disk_offset >= drive->getCapacity(component, drive->userdata)) {
|
||||||
nn_setCError(computer, "bad argument #1 (index out of range)");
|
nn_setCError(computer, "bad argument #1 (index out of range)");
|
||||||
@ -96,11 +96,11 @@ void nn_drive_readByte(nn_drive *drive, void *_, nn_component *component, nn_com
|
|||||||
void nn_drive_writeByte(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
|
void nn_drive_writeByte(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
|
||||||
nn_value offsetValue = nn_getArgument(computer, 0);
|
nn_value offsetValue = nn_getArgument(computer, 0);
|
||||||
nn_value writeValue = nn_getArgument(computer, 1);
|
nn_value writeValue = nn_getArgument(computer, 1);
|
||||||
size_t disk_offset = nn_toInt(offsetValue) - 1;
|
nn_size_t disk_offset = nn_toInt(offsetValue) - 1;
|
||||||
intptr_t write = nn_toInt(writeValue);
|
nn_intptr_t write = nn_toInt(writeValue);
|
||||||
size_t sector_size = drive->getSectorSize(component, drive->userdata);
|
nn_size_t sector_size = drive->getSectorSize(component, drive->userdata);
|
||||||
int sector = (disk_offset / sector_size) + 1;
|
int sector = (disk_offset / sector_size) + 1;
|
||||||
size_t sector_offset = disk_offset % sector_size;
|
nn_size_t sector_offset = disk_offset % sector_size;
|
||||||
|
|
||||||
if (write < -128 || write > 255) {
|
if (write < -128 || write > 255) {
|
||||||
nn_setCError(computer, "bad argument #2 (byte out of range)");
|
nn_setCError(computer, "bad argument #2 (byte out of range)");
|
||||||
|
@ -4,7 +4,7 @@ nn_eepromControl nn_eeprom_getControl(nn_component *component, nn_eeprom *eeprom
|
|||||||
return eeprom->control(component, eeprom->userdata);
|
return eeprom->control(component, eeprom->userdata);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void nn_eeprom_readCost(nn_component *component, size_t bytesRead) {
|
static void nn_eeprom_readCost(nn_component *component, nn_size_t bytesRead) {
|
||||||
nn_eepromControl control = nn_eeprom_getControl(component, nn_getComponentUserdata(component));
|
nn_eepromControl control = nn_eeprom_getControl(component, nn_getComponentUserdata(component));
|
||||||
nn_computer *computer = nn_getComputerOfComponent(component);
|
nn_computer *computer = nn_getComputerOfComponent(component);
|
||||||
|
|
||||||
@ -13,7 +13,7 @@ static void nn_eeprom_readCost(nn_component *component, size_t bytesRead) {
|
|||||||
nn_simulateBufferedIndirect(component, bytesRead, control.bytesReadPerTick);
|
nn_simulateBufferedIndirect(component, bytesRead, control.bytesReadPerTick);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void nn_eeprom_writeCost(nn_component *component, size_t bytesWritten) {
|
static void nn_eeprom_writeCost(nn_component *component, nn_size_t bytesWritten) {
|
||||||
nn_eepromControl control = nn_eeprom_getControl(component, nn_getComponentUserdata(component));
|
nn_eepromControl control = nn_eeprom_getControl(component, nn_getComponentUserdata(component));
|
||||||
nn_computer *computer = nn_getComputerOfComponent(component);
|
nn_computer *computer = nn_getComputerOfComponent(component);
|
||||||
|
|
||||||
@ -40,7 +40,7 @@ void nn_eeprom_getDataSize(nn_eeprom *eeprom, void *_, nn_component *component,
|
|||||||
|
|
||||||
void nn_eeprom_getLabel(nn_eeprom *eeprom, void *_, nn_component *component, nn_computer *computer) {
|
void nn_eeprom_getLabel(nn_eeprom *eeprom, void *_, nn_component *component, nn_computer *computer) {
|
||||||
char buf[NN_LABEL_SIZE];
|
char buf[NN_LABEL_SIZE];
|
||||||
size_t l = NN_LABEL_SIZE;
|
nn_size_t l = NN_LABEL_SIZE;
|
||||||
eeprom->getLabel(component, eeprom->userdata, buf, &l);
|
eeprom->getLabel(component, eeprom->userdata, buf, &l);
|
||||||
if(l == 0) {
|
if(l == 0) {
|
||||||
nn_return(computer, nn_values_nil());
|
nn_return(computer, nn_values_nil());
|
||||||
@ -53,7 +53,7 @@ void nn_eeprom_getLabel(nn_eeprom *eeprom, void *_, nn_component *component, nn_
|
|||||||
}
|
}
|
||||||
|
|
||||||
void nn_eeprom_setLabel(nn_eeprom *eeprom, void *_, nn_component *component, nn_computer *computer) {
|
void nn_eeprom_setLabel(nn_eeprom *eeprom, void *_, nn_component *component, nn_computer *computer) {
|
||||||
size_t l = 0;
|
nn_size_t l = 0;
|
||||||
nn_value label = nn_getArgument(computer, 0);
|
nn_value label = nn_getArgument(computer, 0);
|
||||||
const char *buf = nn_toString(label, &l);
|
const char *buf = nn_toString(label, &l);
|
||||||
if(buf == NULL) {
|
if(buf == NULL) {
|
||||||
@ -68,14 +68,14 @@ void nn_eeprom_setLabel(nn_eeprom *eeprom, void *_, nn_component *component, nn_
|
|||||||
}
|
}
|
||||||
|
|
||||||
void nn_eeprom_get(nn_eeprom *eeprom, void *_, nn_component *component, nn_computer *computer) {
|
void nn_eeprom_get(nn_eeprom *eeprom, void *_, nn_component *component, nn_computer *computer) {
|
||||||
size_t cap = eeprom->getSize(component, eeprom->userdata);
|
nn_size_t cap = eeprom->getSize(component, eeprom->userdata);
|
||||||
nn_Alloc *alloc = nn_getAllocator(nn_getUniverse(computer));
|
nn_Alloc *alloc = nn_getAllocator(nn_getUniverse(computer));
|
||||||
char *buf = nn_alloc(alloc, cap);
|
char *buf = nn_alloc(alloc, cap);
|
||||||
if(buf == NULL) {
|
if(buf == NULL) {
|
||||||
nn_setCError(computer, "out of memory");
|
nn_setCError(computer, "out of memory");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
size_t len = eeprom->get(component, eeprom->userdata, buf);
|
nn_size_t len = eeprom->get(component, eeprom->userdata, buf);
|
||||||
nn_return_string(computer, buf, len);
|
nn_return_string(computer, buf, len);
|
||||||
nn_dealloc(alloc, buf, cap);
|
nn_dealloc(alloc, buf, cap);
|
||||||
|
|
||||||
@ -84,7 +84,7 @@ void nn_eeprom_get(nn_eeprom *eeprom, void *_, nn_component *component, nn_compu
|
|||||||
|
|
||||||
void nn_eeprom_set(nn_eeprom *eeprom, void *_, nn_component *component, nn_computer *computer) {
|
void nn_eeprom_set(nn_eeprom *eeprom, void *_, nn_component *component, nn_computer *computer) {
|
||||||
nn_value data = nn_getArgument(computer, 0);
|
nn_value data = nn_getArgument(computer, 0);
|
||||||
size_t len;
|
nn_size_t len;
|
||||||
const char *buf = nn_toString(data, &len);
|
const char *buf = nn_toString(data, &len);
|
||||||
if(len > eeprom->getSize(component, eeprom->userdata)) {
|
if(len > eeprom->getSize(component, eeprom->userdata)) {
|
||||||
nn_setCError(computer, "out of space");
|
nn_setCError(computer, "out of space");
|
||||||
@ -105,7 +105,7 @@ void nn_eeprom_set(nn_eeprom *eeprom, void *_, nn_component *component, nn_compu
|
|||||||
}
|
}
|
||||||
|
|
||||||
void nn_eeprom_getData(nn_eeprom *eeprom, void *_, nn_component *component, nn_computer *computer) {
|
void nn_eeprom_getData(nn_eeprom *eeprom, void *_, nn_component *component, nn_computer *computer) {
|
||||||
size_t cap = eeprom->getDataSize(component, eeprom->userdata);
|
nn_size_t cap = eeprom->getDataSize(component, eeprom->userdata);
|
||||||
nn_Alloc *alloc = nn_getAllocator(nn_getUniverse(computer));
|
nn_Alloc *alloc = nn_getAllocator(nn_getUniverse(computer));
|
||||||
char *buf = nn_alloc(alloc, cap);
|
char *buf = nn_alloc(alloc, cap);
|
||||||
if(buf == NULL) {
|
if(buf == NULL) {
|
||||||
@ -125,7 +125,7 @@ void nn_eeprom_getData(nn_eeprom *eeprom, void *_, nn_component *component, nn_c
|
|||||||
|
|
||||||
void nn_eeprom_setData(nn_eeprom *eeprom, void *_, nn_component *component, nn_computer *computer) {
|
void nn_eeprom_setData(nn_eeprom *eeprom, void *_, nn_component *component, nn_computer *computer) {
|
||||||
nn_value data = nn_getArgument(computer, 0);
|
nn_value data = nn_getArgument(computer, 0);
|
||||||
size_t len = 0;
|
nn_size_t len = 0;
|
||||||
const char *buf = nn_toString(data, &len);
|
const char *buf = nn_toString(data, &len);
|
||||||
if(buf == NULL) {
|
if(buf == NULL) {
|
||||||
if(data.tag == NN_VALUE_NIL) {
|
if(data.tag == NN_VALUE_NIL) {
|
||||||
@ -154,8 +154,8 @@ void nn_eeprom_makeReadonly(nn_eeprom *eeprom, void *_, nn_component *component,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void nn_eeprom_getChecksum(nn_eeprom *eeprom, void *_, nn_component *component, nn_computer *computer) {
|
void nn_eeprom_getChecksum(nn_eeprom *eeprom, void *_, nn_component *component, nn_computer *computer) {
|
||||||
size_t dataCap = eeprom->getDataSize(component, eeprom->userdata);
|
nn_size_t dataCap = eeprom->getDataSize(component, eeprom->userdata);
|
||||||
size_t codeCap = eeprom->getSize(component, eeprom->userdata);
|
nn_size_t codeCap = eeprom->getSize(component, eeprom->userdata);
|
||||||
nn_Alloc *alloc = nn_getAllocator(nn_getUniverse(computer));
|
nn_Alloc *alloc = nn_getAllocator(nn_getUniverse(computer));
|
||||||
char *buf = nn_alloc(alloc, dataCap + codeCap);
|
char *buf = nn_alloc(alloc, dataCap + codeCap);
|
||||||
if(buf == NULL) {
|
if(buf == NULL) {
|
||||||
|
@ -12,7 +12,7 @@ nn_bool_t nn_fs_illegalPath(const char *path) {
|
|||||||
// absolute disaster
|
// absolute disaster
|
||||||
const char *illegal = "\"\\:*?<>|";
|
const char *illegal = "\"\\:*?<>|";
|
||||||
|
|
||||||
for(size_t i = 0; illegal[i] != '\0'; i++) {
|
for(nn_size_t i = 0; illegal[i] != '\0'; i++) {
|
||||||
if(nn_strchr(path, illegal[i]) != NULL) return true;
|
if(nn_strchr(path, illegal[i]) != NULL) return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -22,28 +22,28 @@ nn_filesystemControl nn_fs_getControl(nn_component *component, nn_filesystem *fs
|
|||||||
return fs->control(component, fs->userdata);
|
return fs->control(component, fs->userdata);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t nn_fs_countChunks(nn_filesystem *fs, size_t bytes, nn_component *component) {
|
nn_size_t nn_fs_countChunks(nn_filesystem *fs, nn_size_t bytes, nn_component *component) {
|
||||||
nn_filesystemControl control = nn_fs_getControl(component, fs);
|
nn_filesystemControl control = nn_fs_getControl(component, fs);
|
||||||
|
|
||||||
size_t chunks = bytes / control.pretendChunkSize;
|
nn_size_t chunks = bytes / control.pretendChunkSize;
|
||||||
if(bytes % control.pretendChunkSize != 0) chunks++;
|
if(bytes % control.pretendChunkSize != 0) chunks++;
|
||||||
return chunks;
|
return chunks;
|
||||||
}
|
}
|
||||||
|
|
||||||
void nn_fs_readCost(nn_filesystem *fs, size_t count, nn_component *component, nn_computer *computer) {
|
void nn_fs_readCost(nn_filesystem *fs, nn_size_t count, nn_component *component, nn_computer *computer) {
|
||||||
nn_filesystemControl control = nn_fs_getControl(component, fs);
|
nn_filesystemControl control = nn_fs_getControl(component, fs);
|
||||||
nn_removeEnergy(computer, control.readEnergyCost * count);
|
nn_removeEnergy(computer, control.readEnergyCost * count);
|
||||||
nn_callCost(computer, control.readCostPerChunk * count);
|
nn_callCost(computer, control.readCostPerChunk * count);
|
||||||
}
|
}
|
||||||
|
|
||||||
void nn_fs_writeCost(nn_filesystem *fs, size_t count, nn_component *component, nn_computer *computer) {
|
void nn_fs_writeCost(nn_filesystem *fs, nn_size_t count, nn_component *component, nn_computer *computer) {
|
||||||
nn_filesystemControl control = nn_fs_getControl(component, fs);
|
nn_filesystemControl control = nn_fs_getControl(component, fs);
|
||||||
nn_removeEnergy(computer, control.writeEnergyCost * count);
|
nn_removeEnergy(computer, control.writeEnergyCost * count);
|
||||||
nn_addHeat(computer, control.writeHeatPerChunk * count);
|
nn_addHeat(computer, control.writeHeatPerChunk * count);
|
||||||
nn_callCost(computer, control.writeCostPerChunk * count);
|
nn_callCost(computer, control.writeCostPerChunk * count);
|
||||||
}
|
}
|
||||||
|
|
||||||
void nn_fs_seekCost(nn_filesystem *fs, size_t count, nn_component *component, nn_computer *computer) {
|
void nn_fs_seekCost(nn_filesystem *fs, nn_size_t count, nn_component *component, nn_computer *computer) {
|
||||||
nn_filesystemControl control = nn_fs_getControl(component, fs);
|
nn_filesystemControl control = nn_fs_getControl(component, fs);
|
||||||
if(control.pretendRPM == 0) return; // disabled, likely SSD
|
if(control.pretendRPM == 0) return; // disabled, likely SSD
|
||||||
double rps = (double)control.pretendRPM / 60;
|
double rps = (double)control.pretendRPM / 60;
|
||||||
@ -56,7 +56,7 @@ void nn_fs_seekCost(nn_filesystem *fs, size_t count, nn_component *component, nn
|
|||||||
|
|
||||||
void nn_fs_getLabel(nn_filesystem *fs, void *_, nn_component *component, nn_computer *computer) {
|
void nn_fs_getLabel(nn_filesystem *fs, void *_, nn_component *component, nn_computer *computer) {
|
||||||
char buf[NN_LABEL_SIZE];
|
char buf[NN_LABEL_SIZE];
|
||||||
size_t l = NN_LABEL_SIZE;
|
nn_size_t l = NN_LABEL_SIZE;
|
||||||
fs->getLabel(component, fs->userdata, buf, &l);
|
fs->getLabel(component, fs->userdata, buf, &l);
|
||||||
if(l == 0) {
|
if(l == 0) {
|
||||||
nn_return(computer, nn_values_nil());
|
nn_return(computer, nn_values_nil());
|
||||||
@ -71,7 +71,7 @@ void nn_fs_getLabel(nn_filesystem *fs, void *_, nn_component *component, nn_comp
|
|||||||
}
|
}
|
||||||
|
|
||||||
void nn_fs_setLabel(nn_filesystem *fs, void *_, nn_component *component, nn_computer *computer) {
|
void nn_fs_setLabel(nn_filesystem *fs, void *_, nn_component *component, nn_computer *computer) {
|
||||||
size_t l = 0;
|
nn_size_t l = 0;
|
||||||
nn_value label = nn_getArgument(computer, 0);
|
nn_value label = nn_getArgument(computer, 0);
|
||||||
const char *buf = nn_toString(label, &l);
|
const char *buf = nn_toString(label, &l);
|
||||||
if(buf == NULL) {
|
if(buf == NULL) {
|
||||||
@ -85,14 +85,14 @@ void nn_fs_setLabel(nn_filesystem *fs, void *_, nn_component *component, nn_comp
|
|||||||
}
|
}
|
||||||
|
|
||||||
void nn_fs_spaceUsed(nn_filesystem *fs, void *_, nn_component *component, nn_computer *computer) {
|
void nn_fs_spaceUsed(nn_filesystem *fs, void *_, nn_component *component, nn_computer *computer) {
|
||||||
size_t space = fs->spaceUsed(component, fs->userdata);
|
nn_size_t space = fs->spaceUsed(component, fs->userdata);
|
||||||
nn_return(computer, nn_values_integer(space));
|
nn_return(computer, nn_values_integer(space));
|
||||||
|
|
||||||
nn_fs_readCost(fs, 1, component, computer);
|
nn_fs_readCost(fs, 1, component, computer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void nn_fs_spaceTotal(nn_filesystem *fs, void *_, nn_component *component, nn_computer *computer) {
|
void nn_fs_spaceTotal(nn_filesystem *fs, void *_, nn_component *component, nn_computer *computer) {
|
||||||
size_t space = fs->spaceUsed(component, fs->userdata);
|
nn_size_t space = fs->spaceUsed(component, fs->userdata);
|
||||||
nn_return(computer, nn_values_integer(space));
|
nn_return(computer, nn_values_integer(space));
|
||||||
|
|
||||||
nn_fs_readCost(fs, 1, component, computer);
|
nn_fs_readCost(fs, 1, component, computer);
|
||||||
@ -116,7 +116,7 @@ void nn_fs_size(nn_filesystem *fs, void *_, nn_component *component, nn_computer
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t byteSize = fs->size(component, fs->userdata, path);
|
nn_size_t byteSize = fs->size(component, fs->userdata, path);
|
||||||
|
|
||||||
nn_return(computer, nn_values_integer(byteSize));
|
nn_return(computer, nn_values_integer(byteSize));
|
||||||
|
|
||||||
@ -154,7 +154,7 @@ void nn_fs_lastModified(nn_filesystem *fs, void *_, nn_component *component, nn_
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t t = fs->lastModified(component, fs->userdata, path);
|
nn_size_t t = fs->lastModified(component, fs->userdata, path);
|
||||||
|
|
||||||
// OpenOS does BULLSHIT with this thing, dividing it by 1000 and expecting it to be
|
// OpenOS does BULLSHIT with this thing, dividing it by 1000 and expecting it to be
|
||||||
// fucking usable as a date, meaning it needs to be an int.
|
// fucking usable as a date, meaning it needs to be an int.
|
||||||
@ -189,7 +189,7 @@ void nn_fs_rename(nn_filesystem *fs, void *_, nn_component *component, nn_comput
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t movedCount = fs->rename(component, fs->userdata, from, to);
|
nn_size_t movedCount = fs->rename(component, fs->userdata, from, to);
|
||||||
nn_return(computer, nn_values_boolean(movedCount > 0));
|
nn_return(computer, nn_values_boolean(movedCount > 0));
|
||||||
|
|
||||||
// Considered 2 safety checks + 1 read per file + 1 write per file
|
// Considered 2 safety checks + 1 read per file + 1 write per file
|
||||||
@ -263,13 +263,13 @@ void nn_fs_list(nn_filesystem *fs, void *_, nn_component *component, nn_computer
|
|||||||
|
|
||||||
nn_Alloc *alloc = nn_getAllocator(nn_getUniverse(computer));
|
nn_Alloc *alloc = nn_getAllocator(nn_getUniverse(computer));
|
||||||
|
|
||||||
size_t fileCount = 0;
|
nn_size_t fileCount = 0;
|
||||||
char **files = fs->list(alloc, component, fs->userdata, path, &fileCount);
|
char **files = fs->list(alloc, component, fs->userdata, path, &fileCount);
|
||||||
|
|
||||||
if(files != NULL) {
|
if(files != NULL) {
|
||||||
// operation succeeded
|
// operation succeeded
|
||||||
nn_value arr = nn_values_array(alloc, fileCount);
|
nn_value arr = nn_values_array(alloc, fileCount);
|
||||||
for(size_t i = 0; i < fileCount; i++) {
|
for(nn_size_t i = 0; i < fileCount; i++) {
|
||||||
nn_values_set(arr, i, nn_values_string(alloc, files[i], nn_strlen(files[i])));
|
nn_values_set(arr, i, nn_values_string(alloc, files[i], nn_strlen(files[i])));
|
||||||
nn_deallocStr(alloc, files[i]);
|
nn_deallocStr(alloc, files[i]);
|
||||||
}
|
}
|
||||||
@ -298,7 +298,7 @@ void nn_fs_open(nn_filesystem *fs, void *_, nn_component *component, nn_computer
|
|||||||
mode = "r";
|
mode = "r";
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t fd = fs->open(component, fs->userdata, path, mode);
|
nn_size_t fd = fs->open(component, fs->userdata, path, mode);
|
||||||
nn_return(computer, nn_values_integer(fd));
|
nn_return(computer, nn_values_integer(fd));
|
||||||
|
|
||||||
// 1 safety check
|
// 1 safety check
|
||||||
@ -307,7 +307,7 @@ void nn_fs_open(nn_filesystem *fs, void *_, nn_component *component, nn_computer
|
|||||||
|
|
||||||
void nn_fs_close(nn_filesystem *fs, void *_, nn_component *component, nn_computer *computer) {
|
void nn_fs_close(nn_filesystem *fs, void *_, nn_component *component, nn_computer *computer) {
|
||||||
nn_value fdValue = nn_getArgument(computer, 0);
|
nn_value fdValue = nn_getArgument(computer, 0);
|
||||||
size_t fd = nn_toInt(fdValue);
|
nn_size_t fd = nn_toInt(fdValue);
|
||||||
|
|
||||||
nn_bool_t closed = fs->close(component, fs->userdata, fd);
|
nn_bool_t closed = fs->close(component, fs->userdata, fd);
|
||||||
nn_return(computer, nn_values_boolean(closed));
|
nn_return(computer, nn_values_boolean(closed));
|
||||||
@ -318,12 +318,12 @@ void nn_fs_close(nn_filesystem *fs, void *_, nn_component *component, nn_compute
|
|||||||
|
|
||||||
void nn_fs_write(nn_filesystem *fs, void *_, nn_component *component, nn_computer *computer) {
|
void nn_fs_write(nn_filesystem *fs, void *_, nn_component *component, nn_computer *computer) {
|
||||||
nn_value fdValue = nn_getArgument(computer, 0);
|
nn_value fdValue = nn_getArgument(computer, 0);
|
||||||
size_t fd = nn_toInt(fdValue);
|
nn_size_t fd = nn_toInt(fdValue);
|
||||||
|
|
||||||
// size_t spaceRemaining = fs->spaceTotal(component, fs->userdata) - fs->spaceUsed(component, fs->userdata);
|
// size_t spaceRemaining = fs->spaceTotal(component, fs->userdata) - fs->spaceUsed(component, fs->userdata);
|
||||||
|
|
||||||
nn_value bufferValue = nn_getArgument(computer, 1);
|
nn_value bufferValue = nn_getArgument(computer, 1);
|
||||||
size_t len = 0;
|
nn_size_t len = 0;
|
||||||
const char *buf = nn_toString(bufferValue, &len);
|
const char *buf = nn_toString(bufferValue, &len);
|
||||||
if(buf == NULL) {
|
if(buf == NULL) {
|
||||||
nn_setCError(computer, "bad buffer (string expected)");
|
nn_setCError(computer, "bad buffer (string expected)");
|
||||||
@ -344,9 +344,9 @@ void nn_fs_read(nn_filesystem *fs, void *_, nn_component *component, nn_computer
|
|||||||
|
|
||||||
nn_value lenValue = nn_getArgument(computer, 1);
|
nn_value lenValue = nn_getArgument(computer, 1);
|
||||||
double len = nn_toNumber(lenValue);
|
double len = nn_toNumber(lenValue);
|
||||||
size_t capacity = fs->spaceTotal(component, fs->userdata);
|
nn_size_t capacity = fs->spaceTotal(component, fs->userdata);
|
||||||
if(len > capacity) len = capacity;
|
if(len > capacity) len = capacity;
|
||||||
size_t byteLen = len;
|
nn_size_t byteLen = len;
|
||||||
|
|
||||||
nn_Alloc *alloc = nn_getAllocator(nn_getUniverse(computer));
|
nn_Alloc *alloc = nn_getAllocator(nn_getUniverse(computer));
|
||||||
char *buf = nn_alloc(alloc, byteLen);
|
char *buf = nn_alloc(alloc, byteLen);
|
||||||
@ -355,7 +355,7 @@ void nn_fs_read(nn_filesystem *fs, void *_, nn_component *component, nn_computer
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t readLen = fs->read(component, fs->userdata, fd, buf, byteLen);
|
nn_size_t readLen = fs->read(component, fs->userdata, fd, buf, byteLen);
|
||||||
if(readLen > 0) {
|
if(readLen > 0) {
|
||||||
// Nothing read means EoF.
|
// Nothing read means EoF.
|
||||||
nn_return_string(computer, buf, readLen);
|
nn_return_string(computer, buf, readLen);
|
||||||
@ -375,7 +375,7 @@ nn_bool_t nn_fs_validWhence(const char *s) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void nn_fs_seek(nn_filesystem *fs, void *_, nn_component *component, nn_computer *computer) {
|
void nn_fs_seek(nn_filesystem *fs, void *_, nn_component *component, nn_computer *computer) {
|
||||||
size_t fd = nn_toInt(nn_getArgument(computer, 0));
|
nn_size_t fd = nn_toInt(nn_getArgument(computer, 0));
|
||||||
|
|
||||||
const char *whence = nn_toCString(nn_getArgument(computer, 1));
|
const char *whence = nn_toCString(nn_getArgument(computer, 1));
|
||||||
|
|
||||||
@ -394,7 +394,7 @@ void nn_fs_seek(nn_filesystem *fs, void *_, nn_component *component, nn_computer
|
|||||||
// size_t capacity = fs->spaceTotal(component, fs->userdata);
|
// size_t capacity = fs->spaceTotal(component, fs->userdata);
|
||||||
int moved = 0;
|
int moved = 0;
|
||||||
|
|
||||||
size_t pos = fs->seek(component, fs->userdata, fd, whence, off, &moved);
|
nn_size_t pos = fs->seek(component, fs->userdata, fd, whence, off, &moved);
|
||||||
if(moved < 0) moved = -moved;
|
if(moved < 0) moved = -moved;
|
||||||
|
|
||||||
// do not ask where it comes from, balance is hard
|
// do not ask where it comes from, balance is hard
|
||||||
|
@ -99,12 +99,12 @@ void nni_gpu_bind(nni_gpu *gpu, void *_, nn_component *component, nn_computer *c
|
|||||||
|
|
||||||
|
|
||||||
if(reset) {
|
if(reset) {
|
||||||
for(size_t i = 0; i < screen->width; i++) {
|
for(nn_size_t i = 0; i < screen->width; i++) {
|
||||||
for(size_t j = 0; j < screen->height; j++) {
|
for(nn_size_t j = 0; j < screen->height; j++) {
|
||||||
nn_setPixel(screen, i, j, nni_gpu_makePixel(gpu, " "));
|
nn_setPixel(screen, i, j, nni_gpu_makePixel(gpu, " "));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
size_t area = screen->width * screen->height;
|
nn_size_t area = screen->width * screen->height;
|
||||||
nn_addHeat(computer, gpu->ctrl.heatPerPixelReset * area);
|
nn_addHeat(computer, gpu->ctrl.heatPerPixelReset * area);
|
||||||
nn_simulateBufferedIndirect(component, 1, gpu->ctrl.screenFillPerTick);
|
nn_simulateBufferedIndirect(component, 1, gpu->ctrl.screenFillPerTick);
|
||||||
nn_removeEnergy(computer, gpu->ctrl.energyPerPixelReset * area);
|
nn_removeEnergy(computer, gpu->ctrl.energyPerPixelReset * area);
|
||||||
@ -159,7 +159,7 @@ void nni_gpu_get(nni_gpu *gpu, void *_, nn_component *component, nn_computer *co
|
|||||||
int y = nn_toInt(nn_getArgument(computer, 1)) - 1;
|
int y = nn_toInt(nn_getArgument(computer, 1)) - 1;
|
||||||
nn_scrchr_t pxl = nn_getPixel(gpu->currentScreen, x, y);
|
nn_scrchr_t pxl = nn_getPixel(gpu->currentScreen, x, y);
|
||||||
|
|
||||||
size_t l;
|
nn_size_t l;
|
||||||
char chr[NN_MAXIMUM_UNICODE_BUFFER];
|
char chr[NN_MAXIMUM_UNICODE_BUFFER];
|
||||||
nn_unicode_codepointToChar(chr, pxl.codepoint, &l);
|
nn_unicode_codepointToChar(chr, pxl.codepoint, &l);
|
||||||
|
|
||||||
|
@ -93,8 +93,8 @@ void nn_addKeyboard(nn_screen *screen, nn_address address) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void nn_removeKeyboard(nn_screen *screen, nn_address address) {
|
void nn_removeKeyboard(nn_screen *screen, nn_address address) {
|
||||||
size_t j = 0;
|
nn_size_t j = 0;
|
||||||
for(size_t i = 0; i < screen->keyboardCount; i++) {
|
for(nn_size_t i = 0; i < screen->keyboardCount; i++) {
|
||||||
if(nn_strcmp(screen->keyboards[i], address) == 0) {
|
if(nn_strcmp(screen->keyboards[i], address) == 0) {
|
||||||
nn_deallocStr(&screen->ctx.allocator, screen->keyboards[i]);
|
nn_deallocStr(&screen->ctx.allocator, screen->keyboards[i]);
|
||||||
} else {
|
} else {
|
||||||
@ -105,12 +105,12 @@ void nn_removeKeyboard(nn_screen *screen, nn_address address) {
|
|||||||
screen->keyboardCount = j;
|
screen->keyboardCount = j;
|
||||||
}
|
}
|
||||||
|
|
||||||
nn_address nn_getKeyboard(nn_screen *screen, size_t idx) {
|
nn_address nn_getKeyboard(nn_screen *screen, nn_size_t idx) {
|
||||||
if(idx >= screen->keyboardCount) return NULL;
|
if(idx >= screen->keyboardCount) return NULL;
|
||||||
return screen->keyboards[idx];
|
return screen->keyboards[idx];
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t nn_getKeyboardCount(nn_screen *screen) {
|
nn_size_t nn_getKeyboardCount(nn_screen *screen) {
|
||||||
return screen->keyboardCount;
|
return screen->keyboardCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -213,9 +213,9 @@ void nn_screenComp_getKeyboards(nn_screen *screen, void *_, nn_component *compon
|
|||||||
nn_lockScreen(screen);
|
nn_lockScreen(screen);
|
||||||
nn_value arr = nn_values_array(&screen->ctx.allocator, nn_getKeyboardCount(screen));
|
nn_value arr = nn_values_array(&screen->ctx.allocator, nn_getKeyboardCount(screen));
|
||||||
|
|
||||||
size_t len = arr.array->len;
|
nn_size_t len = arr.array->len;
|
||||||
for(size_t i = 0; i < len; i++) {
|
for(nn_size_t i = 0; i < len; i++) {
|
||||||
size_t addrlen = nn_strlen(nn_getKeyboard(screen, i));
|
nn_size_t addrlen = nn_strlen(nn_getKeyboard(screen, i));
|
||||||
nn_value addr = nn_values_string(&screen->ctx.allocator, nn_getKeyboard(screen, i), addrlen);
|
nn_value addr = nn_values_string(&screen->ctx.allocator, nn_getKeyboard(screen, i), addrlen);
|
||||||
nn_values_set(arr, i, addr);
|
nn_values_set(arr, i, addr);
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ typedef struct nn_screen {
|
|||||||
nn_bool_t isPrecise;
|
nn_bool_t isPrecise;
|
||||||
nn_bool_t isDirty;
|
nn_bool_t isDirty;
|
||||||
nn_address keyboards[NN_MAX_SCREEN_KEYBOARDS];
|
nn_address keyboards[NN_MAX_SCREEN_KEYBOARDS];
|
||||||
size_t keyboardCount;
|
nn_size_t keyboardCount;
|
||||||
} nn_screen;
|
} nn_screen;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
#include "universe.h"
|
#include "universe.h"
|
||||||
#include "neonucleus.h"
|
#include "neonucleus.h"
|
||||||
|
|
||||||
nn_computer *nn_newComputer(nn_universe *universe, nn_address address, nn_architecture *arch, void *userdata, size_t memoryLimit, size_t componentLimit) {
|
nn_computer *nn_newComputer(nn_universe *universe, nn_address address, nn_architecture *arch, void *userdata, nn_size_t memoryLimit, nn_size_t componentLimit) {
|
||||||
nn_Alloc *alloc = &universe->ctx.allocator;
|
nn_Alloc *alloc = &universe->ctx.allocator;
|
||||||
nn_computer *c = nn_alloc(alloc, sizeof(nn_computer));
|
nn_computer *c = nn_alloc(alloc, sizeof(nn_computer));
|
||||||
c->components = nn_alloc(alloc, sizeof(nn_component) * componentLimit);
|
c->components = nn_alloc(alloc, sizeof(nn_component) * componentLimit);
|
||||||
@ -90,11 +90,11 @@ double nn_getUptime(nn_computer *computer) {
|
|||||||
return nn_getTime(computer->universe) - computer->timeOffset;
|
return nn_getTime(computer->universe) - computer->timeOffset;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t nn_getComputerMemoryUsed(nn_computer *computer) {
|
nn_size_t nn_getComputerMemoryUsed(nn_computer *computer) {
|
||||||
return computer->arch->getMemoryUsage(computer, computer->archState, computer->arch->userdata);
|
return computer->arch->getMemoryUsage(computer, computer->archState, computer->arch->userdata);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t nn_getComputerMemoryTotal(nn_computer *computer) {
|
nn_size_t nn_getComputerMemoryTotal(nn_computer *computer) {
|
||||||
return computer->memoryTotal;
|
return computer->memoryTotal;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,7 +108,7 @@ void nn_addSupportedArchitecture(nn_computer *computer, nn_architecture *arch) {
|
|||||||
computer->supportedArchCount++;
|
computer->supportedArchCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
nn_architecture *nn_getSupportedArchitecture(nn_computer *computer, size_t idx) {
|
nn_architecture *nn_getSupportedArchitecture(nn_computer *computer, nn_size_t idx) {
|
||||||
if(idx >= computer->supportedArchCount) return NULL;
|
if(idx >= computer->supportedArchCount) return NULL;
|
||||||
return computer->supportedArch[idx];
|
return computer->supportedArch[idx];
|
||||||
}
|
}
|
||||||
@ -132,7 +132,7 @@ void nn_deleteComputer(nn_computer *computer) {
|
|||||||
nn_popSignal(computer);
|
nn_popSignal(computer);
|
||||||
}
|
}
|
||||||
nn_Alloc *a = &computer->universe->ctx.allocator;
|
nn_Alloc *a = &computer->universe->ctx.allocator;
|
||||||
for(size_t i = 0; i < computer->userCount; i++) {
|
for(nn_size_t i = 0; i < computer->userCount; i++) {
|
||||||
nn_deallocStr(a, computer->users[i]);
|
nn_deallocStr(a, computer->users[i]);
|
||||||
}
|
}
|
||||||
computer->arch->teardown(computer, computer->archState, computer->arch->userdata);
|
computer->arch->teardown(computer, computer->archState, computer->arch->userdata);
|
||||||
@ -143,7 +143,7 @@ void nn_deleteComputer(nn_computer *computer) {
|
|||||||
nn_dealloc(a, computer, sizeof(nn_computer));
|
nn_dealloc(a, computer, sizeof(nn_computer));
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *nn_pushSignal(nn_computer *computer, nn_value *values, size_t len) {
|
const char *nn_pushSignal(nn_computer *computer, nn_value *values, nn_size_t len) {
|
||||||
if(len > NN_MAX_SIGNAL_VALS) return "too many values";
|
if(len > NN_MAX_SIGNAL_VALS) return "too many values";
|
||||||
if(len == 0) return "missing event";
|
if(len == 0) return "missing event";
|
||||||
// no OOM for you hehe
|
// no OOM for you hehe
|
||||||
@ -152,21 +152,21 @@ const char *nn_pushSignal(nn_computer *computer, nn_value *values, size_t len) {
|
|||||||
}
|
}
|
||||||
if(computer->signalCount == NN_MAX_SIGNALS) return "too many signals";
|
if(computer->signalCount == NN_MAX_SIGNALS) return "too many signals";
|
||||||
computer->signals[computer->signalCount].len = len;
|
computer->signals[computer->signalCount].len = len;
|
||||||
for(size_t i = 0; i < len; i++) {
|
for(nn_size_t i = 0; i < len; i++) {
|
||||||
computer->signals[computer->signalCount].values[i] = values[i];
|
computer->signals[computer->signalCount].values[i] = values[i];
|
||||||
}
|
}
|
||||||
computer->signalCount++;
|
computer->signalCount++;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
nn_value nn_fetchSignalValue(nn_computer *computer, size_t index) {
|
nn_value nn_fetchSignalValue(nn_computer *computer, nn_size_t index) {
|
||||||
if(computer->signalCount == 0) return nn_values_nil();
|
if(computer->signalCount == 0) return nn_values_nil();
|
||||||
nn_signal *p = computer->signals;
|
nn_signal *p = computer->signals;
|
||||||
if(index >= p->len) return nn_values_nil();
|
if(index >= p->len) return nn_values_nil();
|
||||||
return p->values[index];
|
return p->values[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t nn_signalSize(nn_computer *computer) {
|
nn_size_t nn_signalSize(nn_computer *computer) {
|
||||||
if(computer->signalCount == 0) return 0;
|
if(computer->signalCount == 0) return 0;
|
||||||
return computer->signals[0].len;
|
return computer->signals[0].len;
|
||||||
}
|
}
|
||||||
@ -174,10 +174,10 @@ size_t nn_signalSize(nn_computer *computer) {
|
|||||||
void nn_popSignal(nn_computer *computer) {
|
void nn_popSignal(nn_computer *computer) {
|
||||||
if(computer->signalCount == 0) return;
|
if(computer->signalCount == 0) return;
|
||||||
nn_signal *p = computer->signals;
|
nn_signal *p = computer->signals;
|
||||||
for(size_t i = 0; i < p->len; i++) {
|
for(nn_size_t i = 0; i < p->len; i++) {
|
||||||
nn_values_drop(p->values[i]);
|
nn_values_drop(p->values[i]);
|
||||||
}
|
}
|
||||||
for(size_t i = 1; i < computer->signalCount; i++) {
|
for(nn_size_t i = 1; i < computer->signalCount; i++) {
|
||||||
computer->signals[i-1] = computer->signals[i];
|
computer->signals[i-1] = computer->signals[i];
|
||||||
}
|
}
|
||||||
computer->signalCount--;
|
computer->signalCount--;
|
||||||
@ -193,8 +193,8 @@ const char *nn_addUser(nn_computer *computer, const char *name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void nn_deleteUser(nn_computer *computer, const char *name) {
|
void nn_deleteUser(nn_computer *computer, const char *name) {
|
||||||
size_t j = 0;
|
nn_size_t j = 0;
|
||||||
for(size_t i = 0; i < computer->userCount; i++) {
|
for(nn_size_t i = 0; i < computer->userCount; i++) {
|
||||||
char *user = computer->users[i];
|
char *user = computer->users[i];
|
||||||
if(nn_strcmp(user, name) == 0) {
|
if(nn_strcmp(user, name) == 0) {
|
||||||
nn_deallocStr(&computer->universe->ctx.allocator, user);
|
nn_deallocStr(&computer->universe->ctx.allocator, user);
|
||||||
@ -206,14 +206,14 @@ void nn_deleteUser(nn_computer *computer, const char *name) {
|
|||||||
computer->userCount = j;
|
computer->userCount = j;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *nn_indexUser(nn_computer *computer, size_t idx) {
|
const char *nn_indexUser(nn_computer *computer, nn_size_t idx) {
|
||||||
if(idx >= computer->userCount) return NULL;
|
if(idx >= computer->userCount) return NULL;
|
||||||
return computer->users[idx];
|
return computer->users[idx];
|
||||||
}
|
}
|
||||||
|
|
||||||
nn_bool_t nn_isUser(nn_computer *computer, const char *name) {
|
nn_bool_t nn_isUser(nn_computer *computer, const char *name) {
|
||||||
if(computer->userCount == 0) return true;
|
if(computer->userCount == 0) return true;
|
||||||
for(size_t i = 0; i < computer->userCount; i++) {
|
for(nn_size_t i = 0; i < computer->userCount; i++) {
|
||||||
if(nn_strcmp(computer->users[i], name) == 0) return true;
|
if(nn_strcmp(computer->users[i], name) == 0) return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -355,7 +355,7 @@ void nn_setCError(nn_computer *computer, const char *err) {
|
|||||||
|
|
||||||
nn_component *nn_newComponent(nn_computer *computer, nn_address address, int slot, nn_componentTable *table, void *userdata) {
|
nn_component *nn_newComponent(nn_computer *computer, nn_address address, int slot, nn_componentTable *table, void *userdata) {
|
||||||
nn_component *c = NULL;
|
nn_component *c = NULL;
|
||||||
for(size_t i = 0; i < computer->componentLen; i++) {
|
for(nn_size_t i = 0; i < computer->componentLen; i++) {
|
||||||
if(computer->components[i].address == NULL) {
|
if(computer->components[i].address == NULL) {
|
||||||
c = computer->components + i;
|
c = computer->components + i;
|
||||||
break;
|
break;
|
||||||
@ -381,7 +381,7 @@ nn_component *nn_newComponent(nn_computer *computer, nn_address address, int slo
|
|||||||
}
|
}
|
||||||
|
|
||||||
void nn_removeComponent(nn_computer *computer, nn_address address) {
|
void nn_removeComponent(nn_computer *computer, nn_address address) {
|
||||||
for(size_t i = 0; i < computer->componentLen; i++) {
|
for(nn_size_t i = 0; i < computer->componentLen; i++) {
|
||||||
if(nn_strcmp(computer->components[i].address, address) == 0) {
|
if(nn_strcmp(computer->components[i].address, address) == 0) {
|
||||||
nn_destroyComponent(computer->components + i);
|
nn_destroyComponent(computer->components + i);
|
||||||
}
|
}
|
||||||
@ -397,7 +397,7 @@ void nn_destroyComponent(nn_component *component) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
nn_component *nn_findComponent(nn_computer *computer, nn_address address) {
|
nn_component *nn_findComponent(nn_computer *computer, nn_address address) {
|
||||||
for(size_t i = 0; i < computer->componentLen; i++) {
|
for(nn_size_t i = 0; i < computer->componentLen; i++) {
|
||||||
if(computer->components[i].address == NULL) continue; // empty slot
|
if(computer->components[i].address == NULL) continue; // empty slot
|
||||||
if(nn_strcmp(computer->components[i].address, address) == 0) {
|
if(nn_strcmp(computer->components[i].address, address) == 0) {
|
||||||
return computer->components + i;
|
return computer->components + i;
|
||||||
@ -406,8 +406,8 @@ nn_component *nn_findComponent(nn_computer *computer, nn_address address) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
nn_component *nn_iterComponent(nn_computer *computer, size_t *internalIndex) {
|
nn_component *nn_iterComponent(nn_computer *computer, nn_size_t *internalIndex) {
|
||||||
for(size_t i = *internalIndex; i < computer->componentLen; i++) {
|
for(nn_size_t i = *internalIndex; i < computer->componentLen; i++) {
|
||||||
if(computer->components[i].address == NULL) continue;
|
if(computer->components[i].address == NULL) continue;
|
||||||
*internalIndex = i+1;
|
*internalIndex = i+1;
|
||||||
return computer->components + i;
|
return computer->components + i;
|
||||||
@ -416,11 +416,11 @@ nn_component *nn_iterComponent(nn_computer *computer, size_t *internalIndex) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void nn_resetCall(nn_computer *computer) {
|
void nn_resetCall(nn_computer *computer) {
|
||||||
for(size_t i = 0; i < computer->argc; i++) {
|
for(nn_size_t i = 0; i < computer->argc; i++) {
|
||||||
nn_values_drop(computer->args[i]);
|
nn_values_drop(computer->args[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(size_t i = 0; i < computer->retc; i++) {
|
for(nn_size_t i = 0; i < computer->retc; i++) {
|
||||||
nn_values_drop(computer->rets[i]);
|
nn_values_drop(computer->rets[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -440,29 +440,29 @@ void nn_return(nn_computer *computer, nn_value val) {
|
|||||||
computer->retc++;
|
computer->retc++;
|
||||||
}
|
}
|
||||||
|
|
||||||
nn_value nn_getArgument(nn_computer *computer, size_t idx) {
|
nn_value nn_getArgument(nn_computer *computer, nn_size_t idx) {
|
||||||
if(idx >= computer->argc) return nn_values_nil();
|
if(idx >= computer->argc) return nn_values_nil();
|
||||||
return computer->args[idx];
|
return computer->args[idx];
|
||||||
}
|
}
|
||||||
|
|
||||||
nn_value nn_getReturn(nn_computer *computer, size_t idx) {
|
nn_value nn_getReturn(nn_computer *computer, nn_size_t idx) {
|
||||||
if(idx >= computer->retc) return nn_values_nil();
|
if(idx >= computer->retc) return nn_values_nil();
|
||||||
return computer->rets[idx];
|
return computer->rets[idx];
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t nn_getArgumentCount(nn_computer *computer) {
|
nn_size_t nn_getArgumentCount(nn_computer *computer) {
|
||||||
return computer->argc;
|
return computer->argc;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t nn_getReturnCount(nn_computer *computer) {
|
nn_size_t nn_getReturnCount(nn_computer *computer) {
|
||||||
return computer->retc;
|
return computer->retc;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *nn_serializeProgram(nn_computer *computer, size_t *len) {
|
char *nn_serializeProgram(nn_computer *computer, nn_size_t *len) {
|
||||||
return computer->arch->serialize(computer, computer->archState, computer->arch->userdata, len);
|
return computer->arch->serialize(computer, computer->archState, computer->arch->userdata, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
void nn_deserializeProgram(nn_computer *computer, const char *memory, size_t len) {
|
void nn_deserializeProgram(nn_computer *computer, const char *memory, nn_size_t len) {
|
||||||
computer->arch->deserialize(computer, memory, len, computer->archState, computer->arch->userdata);
|
computer->arch->deserialize(computer, memory, len, computer->archState, computer->arch->userdata);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -478,7 +478,7 @@ void nn_return_nil(nn_computer *computer) {
|
|||||||
nn_return(computer, nn_values_nil());
|
nn_return(computer, nn_values_nil());
|
||||||
}
|
}
|
||||||
|
|
||||||
void nn_return_integer(nn_computer *computer, intptr_t integer) {
|
void nn_return_integer(nn_computer *computer, nn_intptr_t integer) {
|
||||||
nn_return(computer, nn_values_integer(integer));
|
nn_return(computer, nn_values_integer(integer));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -494,7 +494,7 @@ void nn_return_cstring(nn_computer *computer, const char *cstr) {
|
|||||||
nn_return(computer, nn_values_cstring(cstr));
|
nn_return(computer, nn_values_cstring(cstr));
|
||||||
}
|
}
|
||||||
|
|
||||||
void nn_return_string(nn_computer *computer, const char *str, size_t len) {
|
void nn_return_string(nn_computer *computer, const char *str, nn_size_t len) {
|
||||||
nn_value val = nn_values_string(&computer->universe->ctx.allocator, str, len);
|
nn_value val = nn_values_string(&computer->universe->ctx.allocator, str, len);
|
||||||
if(val.tag == NN_VALUE_NIL) {
|
if(val.tag == NN_VALUE_NIL) {
|
||||||
nn_setCError(computer, "out of memory");
|
nn_setCError(computer, "out of memory");
|
||||||
@ -502,7 +502,7 @@ void nn_return_string(nn_computer *computer, const char *str, size_t len) {
|
|||||||
nn_return(computer, val);
|
nn_return(computer, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
nn_value nn_return_array(nn_computer *computer, size_t len) {
|
nn_value nn_return_array(nn_computer *computer, nn_size_t len) {
|
||||||
nn_value val = nn_values_array(&computer->universe->ctx.allocator, len);
|
nn_value val = nn_values_array(&computer->universe->ctx.allocator, len);
|
||||||
if(val.tag == NN_VALUE_NIL) {
|
if(val.tag == NN_VALUE_NIL) {
|
||||||
nn_setCError(computer, "out of memory");
|
nn_setCError(computer, "out of memory");
|
||||||
@ -511,7 +511,7 @@ nn_value nn_return_array(nn_computer *computer, size_t len) {
|
|||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
nn_value nn_return_table(nn_computer *computer, size_t len) {
|
nn_value nn_return_table(nn_computer *computer, nn_size_t len) {
|
||||||
nn_value val = nn_values_table(&computer->universe->ctx.allocator, len);
|
nn_value val = nn_values_table(&computer->universe->ctx.allocator, len);
|
||||||
if(val.tag == NN_VALUE_NIL) {
|
if(val.tag == NN_VALUE_NIL) {
|
||||||
nn_setCError(computer, "out of memory");
|
nn_setCError(computer, "out of memory");
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
#include "neonucleus.h"
|
#include "neonucleus.h"
|
||||||
|
|
||||||
typedef struct nn_signal {
|
typedef struct nn_signal {
|
||||||
size_t len;
|
nn_size_t len;
|
||||||
nn_value values[NN_MAX_SIGNAL_VALS];
|
nn_value values[NN_MAX_SIGNAL_VALS];
|
||||||
} nn_signal;
|
} nn_signal;
|
||||||
|
|
||||||
@ -15,26 +15,26 @@ typedef struct nn_computer {
|
|||||||
void *userdata;
|
void *userdata;
|
||||||
nn_guard *lock;
|
nn_guard *lock;
|
||||||
nn_component *components;
|
nn_component *components;
|
||||||
size_t componentLen;
|
nn_size_t componentLen;
|
||||||
size_t componentCap;
|
nn_size_t componentCap;
|
||||||
nn_value args[NN_MAX_ARGS];
|
nn_value args[NN_MAX_ARGS];
|
||||||
size_t argc;
|
nn_size_t argc;
|
||||||
nn_value rets[NN_MAX_RETS];
|
nn_value rets[NN_MAX_RETS];
|
||||||
size_t retc;
|
nn_size_t retc;
|
||||||
nn_architecture *arch;
|
nn_architecture *arch;
|
||||||
void *archState;
|
void *archState;
|
||||||
nn_architecture *nextArch;
|
nn_architecture *nextArch;
|
||||||
nn_architecture *supportedArch[NN_MAX_ARCHITECTURES];
|
nn_architecture *supportedArch[NN_MAX_ARCHITECTURES];
|
||||||
size_t supportedArchCount;
|
nn_size_t supportedArchCount;
|
||||||
double timeOffset;
|
double timeOffset;
|
||||||
nn_universe *universe;
|
nn_universe *universe;
|
||||||
char *users[NN_MAX_USERS];
|
char *users[NN_MAX_USERS];
|
||||||
size_t userCount;
|
nn_size_t userCount;
|
||||||
double energy;
|
double energy;
|
||||||
double maxEnergy;
|
double maxEnergy;
|
||||||
nn_signal signals[NN_MAX_SIGNALS];
|
nn_signal signals[NN_MAX_SIGNALS];
|
||||||
size_t signalCount;
|
nn_size_t signalCount;
|
||||||
size_t memoryTotal;
|
nn_size_t memoryTotal;
|
||||||
nn_address address;
|
nn_address address;
|
||||||
nn_address tmpAddress;
|
nn_address tmpAddress;
|
||||||
double temperature;
|
double temperature;
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "neonucleus.h"
|
#include "neonucleus.h"
|
||||||
|
@ -69,7 +69,7 @@ void nn_deleteGuard(nn_Context *context, nn_guard *guard) {
|
|||||||
nn_dealloc(&context->allocator, guard, context->lockManager.lockSize);
|
nn_dealloc(&context->allocator, guard, context->lockManager.lockSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
void nn_addRef(nn_refc *refc, size_t count) {
|
void nn_addRef(nn_refc *refc, nn_size_t count) {
|
||||||
(*refc) += count;
|
(*refc) += count;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,7 +77,7 @@ void nn_incRef(nn_refc *refc) {
|
|||||||
nn_addRef(refc, 1);
|
nn_addRef(refc, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
nn_bool_t nn_removeRef(nn_refc *refc, size_t count) {
|
nn_bool_t nn_removeRef(nn_refc *refc, nn_size_t count) {
|
||||||
return ((*refc) -= count) == 0;
|
return ((*refc) -= count) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
265
src/neonucleus.h
265
src/neonucleus.h
@ -1,11 +1,31 @@
|
|||||||
#ifndef NEONUCLEUS_H
|
#ifndef NEONUCLEUS_H
|
||||||
#define NEONUCLEUS_H
|
#define NEONUCLEUS_H
|
||||||
|
|
||||||
|
#ifndef NULL
|
||||||
|
#ifdef __cplusplus
|
||||||
|
#define NULL nullptr
|
||||||
|
#else
|
||||||
|
#define NULL ((void *)0)
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef NN_BAREMETAL
|
||||||
|
#if defined(__LP64__) || defined(_LP64)
|
||||||
|
// long is ptr sized
|
||||||
|
typedef long nn_intptr_t;
|
||||||
|
typedef unsigned long nn_size_t;
|
||||||
|
#else
|
||||||
|
typedef long long nn_intptr_t;
|
||||||
|
typedef unsigned long long nn_size_t;
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#ifndef NN_NEONUCLEUS
|
typedef intptr_t nn_intptr_t;
|
||||||
#include <stdbool.h>
|
typedef size_t nn_size_t;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef bool
|
#ifdef bool
|
||||||
@ -35,10 +55,6 @@ typedef unsigned char nn_bool_t;
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Based off https://stackoverflow.com/questions/5919996/how-to-detect-reliably-mac-os-x-ios-linux-windows-in-c-preprocessor
|
// Based off https://stackoverflow.com/questions/5919996/how-to-detect-reliably-mac-os-x-ios-linux-windows-in-c-preprocessor
|
||||||
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
|
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
|
||||||
//define something for Windows (32-bit and 64-bit, this part is common)
|
//define something for Windows (32-bit and 64-bit, this part is common)
|
||||||
@ -76,6 +92,11 @@ extern "C" {
|
|||||||
#define NN_POSIX
|
#define NN_POSIX
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
// The entire C API, in one header
|
// The entire C API, in one header
|
||||||
|
|
||||||
// Magic limits
|
// Magic limits
|
||||||
@ -103,9 +124,9 @@ extern "C" {
|
|||||||
|
|
||||||
typedef struct nn_guard nn_guard;
|
typedef struct nn_guard nn_guard;
|
||||||
#ifdef __STDC_NO_ATOMICS__
|
#ifdef __STDC_NO_ATOMICS__
|
||||||
typedef atomic_size_t nn_refc;
|
typedef size_t nn_refc;
|
||||||
#else
|
#else
|
||||||
typedef _Atomic(size_t) nn_refc;
|
typedef _Atomic(nn_size_t) nn_refc;
|
||||||
#endif
|
#endif
|
||||||
typedef struct nn_universe nn_universe;
|
typedef struct nn_universe nn_universe;
|
||||||
typedef struct nn_computer nn_computer;
|
typedef struct nn_computer nn_computer;
|
||||||
@ -116,11 +137,11 @@ typedef struct nn_architecture {
|
|||||||
const char *archName;
|
const char *archName;
|
||||||
void *(*setup)(nn_computer *computer, void *userdata);
|
void *(*setup)(nn_computer *computer, void *userdata);
|
||||||
void (*teardown)(nn_computer *computer, void *state, void *userdata);
|
void (*teardown)(nn_computer *computer, void *state, void *userdata);
|
||||||
size_t (*getMemoryUsage)(nn_computer *computer, void *state, void *userdata);
|
nn_size_t (*getMemoryUsage)(nn_computer *computer, void *state, void *userdata);
|
||||||
void (*tick)(nn_computer *computer, void *state, void *userdata);
|
void (*tick)(nn_computer *computer, void *state, void *userdata);
|
||||||
/* Pointer returned should be allocated with nn_malloc or nn_realloc, so it can be freed with nn_free */
|
/* Pointer returned should be allocated with nn_malloc or nn_realloc, so it can be freed with nn_free */
|
||||||
char *(*serialize)(nn_computer *computer, void *state, void *userdata, size_t *len);
|
char *(*serialize)(nn_computer *computer, void *state, void *userdata, nn_size_t *len);
|
||||||
void (*deserialize)(nn_computer *computer, const char *data, size_t len, void *state, void *userdata);
|
void (*deserialize)(nn_computer *computer, const char *data, nn_size_t len, void *state, void *userdata);
|
||||||
} nn_architecture;
|
} nn_architecture;
|
||||||
typedef char *nn_address;
|
typedef char *nn_address;
|
||||||
|
|
||||||
@ -128,7 +149,7 @@ typedef char *nn_address;
|
|||||||
// A zero malloc is never called, the proc address itself is returned, which is ignored when freeing.
|
// A zero malloc is never called, the proc address itself is returned, which is ignored when freeing.
|
||||||
// A free is a non-null ptr, with a non-zero oldSize, but a newSize of 0.
|
// A free is a non-null ptr, with a non-zero oldSize, but a newSize of 0.
|
||||||
// A realloc is a non-null ptr, with a non-zero oldSize, and a non-zero newSize.
|
// A realloc is a non-null ptr, with a non-zero oldSize, and a non-zero newSize.
|
||||||
typedef void *nn_AllocProc(void *userdata, void *ptr, size_t oldSize, size_t newSize, void *extra);
|
typedef void *nn_AllocProc(void *userdata, void *ptr, nn_size_t oldSize, nn_size_t newSize, void *extra);
|
||||||
|
|
||||||
typedef struct nn_Alloc {
|
typedef struct nn_Alloc {
|
||||||
void *userdata;
|
void *userdata;
|
||||||
@ -147,7 +168,7 @@ typedef nn_bool_t nn_LockProc(void *userdata, void *lock, int action, int flags)
|
|||||||
|
|
||||||
typedef struct nn_LockManager {
|
typedef struct nn_LockManager {
|
||||||
void *userdata;
|
void *userdata;
|
||||||
size_t lockSize;
|
nn_size_t lockSize;
|
||||||
nn_LockProc *proc;
|
nn_LockProc *proc;
|
||||||
} nn_LockManager;
|
} nn_LockManager;
|
||||||
|
|
||||||
@ -158,15 +179,15 @@ typedef struct nn_Clock {
|
|||||||
nn_ClockProc *proc;
|
nn_ClockProc *proc;
|
||||||
} nn_Clock;
|
} nn_Clock;
|
||||||
|
|
||||||
typedef size_t nn_RngProc(void *userdata);
|
typedef nn_size_t nn_RngProc(void *userdata);
|
||||||
|
|
||||||
typedef struct nn_Rng {
|
typedef struct nn_Rng {
|
||||||
void *userdata;
|
void *userdata;
|
||||||
size_t maximum;
|
nn_size_t maximum;
|
||||||
nn_RngProc *proc;
|
nn_RngProc *proc;
|
||||||
} nn_Rng;
|
} nn_Rng;
|
||||||
|
|
||||||
size_t nn_rand(nn_Rng *rng);
|
nn_size_t nn_rand(nn_Rng *rng);
|
||||||
// returns from 0 to 1 (inclusive)
|
// returns from 0 to 1 (inclusive)
|
||||||
double nn_randf(nn_Rng *rng);
|
double nn_randf(nn_Rng *rng);
|
||||||
// returns from 0 to 1 (exclusive)
|
// returns from 0 to 1 (exclusive)
|
||||||
@ -181,12 +202,12 @@ typedef struct nn_Context {
|
|||||||
|
|
||||||
// libc-like utils
|
// libc-like utils
|
||||||
|
|
||||||
void nn_memset(void *buf, unsigned char byte, size_t len);
|
void nn_memset(void *buf, unsigned char byte, nn_size_t len);
|
||||||
void nn_memcpy(void *dest, const void *src, size_t len);
|
void nn_memcpy(void *dest, const void *src, nn_size_t len);
|
||||||
char *nn_strcpy(char *dest, const char *src);
|
char *nn_strcpy(char *dest, const char *src);
|
||||||
const char *nn_strchr(const char *str, int ch);
|
const char *nn_strchr(const char *str, int ch);
|
||||||
int nn_strcmp(const char *a, const char *b);
|
int nn_strcmp(const char *a, const char *b);
|
||||||
size_t nn_strlen(const char *a);
|
nn_size_t nn_strlen(const char *a);
|
||||||
|
|
||||||
#ifndef NN_BAREMETAL
|
#ifndef NN_BAREMETAL
|
||||||
nn_Alloc nn_libcAllocator();
|
nn_Alloc nn_libcAllocator();
|
||||||
@ -211,29 +232,29 @@ nn_LockManager nn_noMutex();
|
|||||||
|
|
||||||
typedef struct nn_string {
|
typedef struct nn_string {
|
||||||
char *data;
|
char *data;
|
||||||
size_t len;
|
nn_size_t len;
|
||||||
size_t refc;
|
nn_size_t refc;
|
||||||
nn_Alloc alloc;
|
nn_Alloc alloc;
|
||||||
} nn_string;
|
} nn_string;
|
||||||
|
|
||||||
typedef struct nn_array {
|
typedef struct nn_array {
|
||||||
struct nn_value *values;
|
struct nn_value *values;
|
||||||
size_t len;
|
nn_size_t len;
|
||||||
size_t refc;
|
nn_size_t refc;
|
||||||
nn_Alloc alloc;
|
nn_Alloc alloc;
|
||||||
} nn_array;
|
} nn_array;
|
||||||
|
|
||||||
typedef struct nn_object {
|
typedef struct nn_object {
|
||||||
struct nn_pair *pairs;
|
struct nn_pair *pairs;
|
||||||
size_t len;
|
nn_size_t len;
|
||||||
size_t refc;
|
nn_size_t refc;
|
||||||
nn_Alloc alloc;
|
nn_Alloc alloc;
|
||||||
} nn_table;
|
} nn_table;
|
||||||
|
|
||||||
typedef struct nn_value {
|
typedef struct nn_value {
|
||||||
size_t tag;
|
nn_size_t tag;
|
||||||
union {
|
union {
|
||||||
intptr_t integer;
|
nn_intptr_t integer;
|
||||||
double number;
|
double number;
|
||||||
nn_bool_t boolean;
|
nn_bool_t boolean;
|
||||||
const char *cstring;
|
const char *cstring;
|
||||||
@ -249,13 +270,13 @@ typedef struct nn_pair {
|
|||||||
} nn_pair;
|
} nn_pair;
|
||||||
|
|
||||||
// we expose the allocator because of some utilities
|
// we expose the allocator because of some utilities
|
||||||
void *nn_alloc(nn_Alloc *alloc, size_t size);
|
void *nn_alloc(nn_Alloc *alloc, nn_size_t size);
|
||||||
void *nn_resize(nn_Alloc *alloc, void *memory, size_t oldSize, size_t newSize);
|
void *nn_resize(nn_Alloc *alloc, void *memory, nn_size_t oldSize, nn_size_t newSize);
|
||||||
void nn_dealloc(nn_Alloc *alloc, void *memory, size_t size);
|
void nn_dealloc(nn_Alloc *alloc, void *memory, nn_size_t size);
|
||||||
|
|
||||||
// Utilities, both internal and external
|
// Utilities, both internal and external
|
||||||
char *nn_strdup(nn_Alloc *alloc, const char *s);
|
char *nn_strdup(nn_Alloc *alloc, const char *s);
|
||||||
void *nn_memdup(nn_Alloc *alloc, const void *buf, size_t len);
|
void *nn_memdup(nn_Alloc *alloc, const void *buf, nn_size_t len);
|
||||||
void nn_deallocStr(nn_Alloc *alloc, char *s);
|
void nn_deallocStr(nn_Alloc *alloc, char *s);
|
||||||
|
|
||||||
nn_guard *nn_newGuard(nn_Context *context);
|
nn_guard *nn_newGuard(nn_Context *context);
|
||||||
@ -264,10 +285,10 @@ nn_bool_t nn_tryLock(nn_Context *context, nn_guard *guard);
|
|||||||
void nn_unlock(nn_Context *context, nn_guard *guard);
|
void nn_unlock(nn_Context *context, nn_guard *guard);
|
||||||
void nn_deleteGuard(nn_Context *context, nn_guard *guard);
|
void nn_deleteGuard(nn_Context *context, nn_guard *guard);
|
||||||
|
|
||||||
void nn_addRef(nn_refc *refc, size_t count);
|
void nn_addRef(nn_refc *refc, nn_size_t count);
|
||||||
void nn_incRef(nn_refc *refc);
|
void nn_incRef(nn_refc *refc);
|
||||||
/* Returns true if the object should be freed */
|
/* Returns true if the object should be freed */
|
||||||
nn_bool_t nn_removeRef(nn_refc *refc, size_t count);
|
nn_bool_t nn_removeRef(nn_refc *refc, nn_size_t count);
|
||||||
/* Returns true if the object should be freed */
|
/* Returns true if the object should be freed */
|
||||||
nn_bool_t nn_decRef(nn_refc *refc);
|
nn_bool_t nn_decRef(nn_refc *refc);
|
||||||
|
|
||||||
@ -275,15 +296,15 @@ nn_bool_t nn_decRef(nn_refc *refc);
|
|||||||
|
|
||||||
nn_bool_t nn_unicode_validate(const char *s);
|
nn_bool_t nn_unicode_validate(const char *s);
|
||||||
// returned string must be nn_deallocStr()'d
|
// returned string must be nn_deallocStr()'d
|
||||||
char *nn_unicode_char(nn_Alloc *alloc, unsigned int *codepoints, size_t codepointCount);
|
char *nn_unicode_char(nn_Alloc *alloc, unsigned int *codepoints, nn_size_t codepointCount);
|
||||||
// returned array must be nn_dealloc()'d
|
// returned array must be nn_dealloc()'d
|
||||||
unsigned int *nn_unicode_codepoints(nn_Alloc *alloc, const char *s, size_t *len);
|
unsigned int *nn_unicode_codepoints(nn_Alloc *alloc, const char *s, nn_size_t *len);
|
||||||
size_t nn_unicode_len(const char *s);
|
nn_size_t nn_unicode_len(const char *s);
|
||||||
unsigned int nn_unicode_codepointAt(const char *s, size_t byteOffset);
|
unsigned int nn_unicode_codepointAt(const char *s, nn_size_t byteOffset);
|
||||||
size_t nn_unicode_codepointSize(unsigned int codepoint);
|
nn_size_t nn_unicode_codepointSize(unsigned int codepoint);
|
||||||
void nn_unicode_codepointToChar(char buffer[NN_MAXIMUM_UNICODE_BUFFER], unsigned int codepoint, size_t *len);
|
void nn_unicode_codepointToChar(char buffer[NN_MAXIMUM_UNICODE_BUFFER], unsigned int codepoint, nn_size_t *len);
|
||||||
size_t nn_unicode_charWidth(unsigned int codepoint);
|
nn_size_t nn_unicode_charWidth(unsigned int codepoint);
|
||||||
size_t nn_unicode_wlen(const char *s);
|
nn_size_t nn_unicode_wlen(const char *s);
|
||||||
unsigned int nn_unicode_upperCodepoint(unsigned int codepoint);
|
unsigned int nn_unicode_upperCodepoint(unsigned int codepoint);
|
||||||
// returned string must be nn_deallocStr()'d
|
// returned string must be nn_deallocStr()'d
|
||||||
char *nn_unicode_upper(nn_Alloc *alloc, const char *s);
|
char *nn_unicode_upper(nn_Alloc *alloc, const char *s);
|
||||||
@ -294,40 +315,40 @@ char *nn_unicode_lower(nn_Alloc *alloc, const char *s);
|
|||||||
// Data card stuff
|
// Data card stuff
|
||||||
|
|
||||||
// Hashing
|
// Hashing
|
||||||
void nn_data_crc32(const char *inBuf, size_t buflen, char outBuf[4]);
|
void nn_data_crc32(const char *inBuf, nn_size_t buflen, char outBuf[4]);
|
||||||
void nn_data_md5(const char *inBuf, size_t buflen, char outBuf[16]);
|
void nn_data_md5(const char *inBuf, nn_size_t buflen, char outBuf[16]);
|
||||||
void nn_data_sha256(const char *inBuf, size_t buflen, char outBuf[32]);
|
void nn_data_sha256(const char *inBuf, nn_size_t buflen, char outBuf[32]);
|
||||||
|
|
||||||
// Base64
|
// Base64
|
||||||
|
|
||||||
// The initial value of *len is the size of buf, with the new value being the length of the returned buffer.
|
// The initial value of *len is the size of buf, with the new value being the length of the returned buffer.
|
||||||
char *nn_data_decode64(nn_Alloc *alloc, const char *buf, size_t *len);
|
char *nn_data_decode64(nn_Alloc *alloc, const char *buf, nn_size_t *len);
|
||||||
char *nn_data_encode64(nn_Alloc *alloc, const char *buf, size_t *len);
|
char *nn_data_encode64(nn_Alloc *alloc, const char *buf, nn_size_t *len);
|
||||||
|
|
||||||
// Deflate/inflate
|
// Deflate/inflate
|
||||||
|
|
||||||
char *nn_data_deflate(nn_Alloc *alloc, const char *buf, size_t *len);
|
char *nn_data_deflate(nn_Alloc *alloc, const char *buf, nn_size_t *len);
|
||||||
char *nn_data_inflate(nn_Alloc *alloc, const char *buf, size_t *len);
|
char *nn_data_inflate(nn_Alloc *alloc, const char *buf, nn_size_t *len);
|
||||||
|
|
||||||
// AES
|
// AES
|
||||||
char *nn_data_aes_encrypt(nn_Alloc *alloc, const char *buf, size_t *len, const char key[16], const char iv[16]);
|
char *nn_data_aes_encrypt(nn_Alloc *alloc, const char *buf, nn_size_t *len, const char key[16], const char iv[16]);
|
||||||
char *nn_data_aes_decrypt(nn_Alloc *alloc, const char *buf, size_t *len, const char key[16], const char iv[16]);
|
char *nn_data_aes_decrypt(nn_Alloc *alloc, const char *buf, nn_size_t *len, const char key[16], const char iv[16]);
|
||||||
|
|
||||||
// ECDH
|
// ECDH
|
||||||
|
|
||||||
// if longKeys is on, instead of taking 32 bytes, the keys take up 48 bytes.
|
// if longKeys is on, instead of taking 32 bytes, the keys take up 48 bytes.
|
||||||
size_t nn_data_ecdh_keylen(nn_bool_t longKeys);
|
nn_size_t nn_data_ecdh_keylen(nn_bool_t longKeys);
|
||||||
// use nn_data_ecdh_keylen to figure out the expected length for the buffers
|
// use nn_data_ecdh_keylen to figure out the expected length for the buffers
|
||||||
void nn_data_ecdh_generateKeyPair(nn_Context *context, nn_bool_t longKeys, char *publicKey, char *privateKey);
|
void nn_data_ecdh_generateKeyPair(nn_Context *context, nn_bool_t longKeys, char *publicKey, char *privateKey);
|
||||||
|
|
||||||
nn_bool_t nn_data_ecdsa_check(nn_bool_t longKeys, const char *buf, size_t buflen, const char *sig, size_t siglen);
|
nn_bool_t nn_data_ecdsa_check(nn_bool_t longKeys, const char *buf, nn_size_t buflen, const char *sig, nn_size_t siglen);
|
||||||
char *nn_data_ecdsa_sign(nn_Alloc *alloc, const char *buf, size_t *buflen, const char *key, nn_bool_t longKeys);
|
char *nn_data_ecdsa_sign(nn_Alloc *alloc, const char *buf, nn_size_t *buflen, const char *key, nn_bool_t longKeys);
|
||||||
|
|
||||||
char *nn_data_ecdh_getSharedKey(nn_Alloc *alloc, size_t *len, const char *privateKey, const char *publicKey, nn_bool_t longKeys);
|
char *nn_data_ecdh_getSharedKey(nn_Alloc *alloc, nn_size_t *len, const char *privateKey, const char *publicKey, nn_bool_t longKeys);
|
||||||
|
|
||||||
// ECC
|
// ECC
|
||||||
char *nn_data_hamming_encode(nn_Alloc *alloc, const char *buf, size_t *len);
|
char *nn_data_hamming_encode(nn_Alloc *alloc, const char *buf, nn_size_t *len);
|
||||||
char *nn_data_hamming_decode(nn_Alloc *alloc, const char *buf, size_t *len);
|
char *nn_data_hamming_decode(nn_Alloc *alloc, const char *buf, nn_size_t *len);
|
||||||
|
|
||||||
// Universe stuff
|
// Universe stuff
|
||||||
|
|
||||||
@ -342,26 +363,26 @@ void *nn_queryUserdata(nn_universe *universe, const char *name);
|
|||||||
void nn_storeUserdata(nn_universe *universe, const char *name, void *data);
|
void nn_storeUserdata(nn_universe *universe, const char *name, void *data);
|
||||||
double nn_getTime(nn_universe *universe);
|
double nn_getTime(nn_universe *universe);
|
||||||
|
|
||||||
nn_computer *nn_newComputer(nn_universe *universe, nn_address address, nn_architecture *arch, void *userdata, size_t memoryLimit, size_t componentLimit);
|
nn_computer *nn_newComputer(nn_universe *universe, nn_address address, nn_architecture *arch, void *userdata, nn_size_t memoryLimit, nn_size_t componentLimit);
|
||||||
nn_universe *nn_getUniverse(nn_computer *computer);
|
nn_universe *nn_getUniverse(nn_computer *computer);
|
||||||
int nn_tickComputer(nn_computer *computer);
|
int nn_tickComputer(nn_computer *computer);
|
||||||
double nn_getUptime(nn_computer *computer);
|
double nn_getUptime(nn_computer *computer);
|
||||||
size_t nn_getComputerMemoryUsed(nn_computer *computer);
|
nn_size_t nn_getComputerMemoryUsed(nn_computer *computer);
|
||||||
size_t nn_getComputerMemoryTotal(nn_computer *computer);
|
nn_size_t nn_getComputerMemoryTotal(nn_computer *computer);
|
||||||
void *nn_getComputerUserData(nn_computer *computer);
|
void *nn_getComputerUserData(nn_computer *computer);
|
||||||
void nn_addSupportedArchitecture(nn_computer *computer, nn_architecture *arch);
|
void nn_addSupportedArchitecture(nn_computer *computer, nn_architecture *arch);
|
||||||
nn_architecture *nn_getSupportedArchitecture(nn_computer *computer, size_t idx);
|
nn_architecture *nn_getSupportedArchitecture(nn_computer *computer, nn_size_t idx);
|
||||||
nn_architecture *nn_getArchitecture(nn_computer *computer);
|
nn_architecture *nn_getArchitecture(nn_computer *computer);
|
||||||
nn_architecture *nn_getNextArchitecture(nn_computer *computer);
|
nn_architecture *nn_getNextArchitecture(nn_computer *computer);
|
||||||
void nn_setNextArchitecture(nn_computer *computer, nn_architecture *arch);
|
void nn_setNextArchitecture(nn_computer *computer, nn_architecture *arch);
|
||||||
void nn_deleteComputer(nn_computer *computer);
|
void nn_deleteComputer(nn_computer *computer);
|
||||||
const char *nn_pushSignal(nn_computer *computer, nn_value *values, size_t len);
|
const char *nn_pushSignal(nn_computer *computer, nn_value *values, nn_size_t len);
|
||||||
nn_value nn_fetchSignalValue(nn_computer *computer, size_t index);
|
nn_value nn_fetchSignalValue(nn_computer *computer, nn_size_t index);
|
||||||
size_t nn_signalSize(nn_computer *computer);
|
nn_size_t nn_signalSize(nn_computer *computer);
|
||||||
void nn_popSignal(nn_computer *computer);
|
void nn_popSignal(nn_computer *computer);
|
||||||
const char *nn_addUser(nn_computer *computer, const char *name);
|
const char *nn_addUser(nn_computer *computer, const char *name);
|
||||||
void nn_deleteUser(nn_computer *computer, const char *name);
|
void nn_deleteUser(nn_computer *computer, const char *name);
|
||||||
const char *nn_indexUser(nn_computer *computer, size_t idx);
|
const char *nn_indexUser(nn_computer *computer, nn_size_t idx);
|
||||||
nn_bool_t nn_isUser(nn_computer *computer, const char *name);
|
nn_bool_t nn_isUser(nn_computer *computer, const char *name);
|
||||||
void nn_setCallBudget(nn_computer *computer, double callBudget);
|
void nn_setCallBudget(nn_computer *computer, double callBudget);
|
||||||
double nn_getCallBudget(nn_computer *computer);
|
double nn_getCallBudget(nn_computer *computer);
|
||||||
@ -371,8 +392,8 @@ nn_bool_t nn_isOverworked(nn_computer *computer);
|
|||||||
void nn_triggerIndirect(nn_computer *computer);
|
void nn_triggerIndirect(nn_computer *computer);
|
||||||
|
|
||||||
/* The memory returned can be freed with nn_free() */
|
/* The memory returned can be freed with nn_free() */
|
||||||
char *nn_serializeProgram(nn_computer *computer, size_t *len);
|
char *nn_serializeProgram(nn_computer *computer, nn_size_t *len);
|
||||||
void nn_deserializeProgram(nn_computer *computer, const char *memory, size_t len);
|
void nn_deserializeProgram(nn_computer *computer, const char *memory, nn_size_t len);
|
||||||
|
|
||||||
void nn_lockComputer(nn_computer *computer);
|
void nn_lockComputer(nn_computer *computer);
|
||||||
void nn_unlockComputer(nn_computer *computer);
|
void nn_unlockComputer(nn_computer *computer);
|
||||||
@ -460,7 +481,7 @@ nn_component *nn_findComponent(nn_computer *computer, nn_address address);
|
|||||||
// an internal structure. YOU SHOULD NOT ADD OR REMOVE COMPONENTS WHILE ITERATING.
|
// an internal structure. YOU SHOULD NOT ADD OR REMOVE COMPONENTS WHILE ITERATING.
|
||||||
// the internalIndex SHOULD BE INITIALIZED TO 0.
|
// the internalIndex SHOULD BE INITIALIZED TO 0.
|
||||||
// Returns NULL at the end
|
// Returns NULL at the end
|
||||||
nn_component *nn_iterComponent(nn_computer *computer, size_t *internalIndex);
|
nn_component *nn_iterComponent(nn_computer *computer, nn_size_t *internalIndex);
|
||||||
|
|
||||||
// Component VTable stuff
|
// Component VTable stuff
|
||||||
|
|
||||||
@ -471,7 +492,7 @@ typedef void nn_componentMethod(void *componentUserdata, void *methodUserdata, n
|
|||||||
nn_componentTable *nn_newComponentTable(nn_Alloc *alloc, const char *typeName, void *userdata, nn_componentConstructor *constructor, nn_componentDestructor *destructor);
|
nn_componentTable *nn_newComponentTable(nn_Alloc *alloc, const char *typeName, void *userdata, nn_componentConstructor *constructor, nn_componentDestructor *destructor);
|
||||||
void nn_destroyComponentTable(nn_componentTable *table);
|
void nn_destroyComponentTable(nn_componentTable *table);
|
||||||
void nn_defineMethod(nn_componentTable *table, const char *methodName, nn_bool_t direct, nn_componentMethod *methodFunc, void *methodUserdata, const char *methodDoc);
|
void nn_defineMethod(nn_componentTable *table, const char *methodName, nn_bool_t direct, nn_componentMethod *methodFunc, void *methodUserdata, const char *methodDoc);
|
||||||
const char *nn_getTableMethod(nn_componentTable *table, size_t idx, nn_bool_t *outDirect);
|
const char *nn_getTableMethod(nn_componentTable *table, nn_size_t idx, nn_bool_t *outDirect);
|
||||||
const char *nn_methodDoc(nn_componentTable *table, const char *methodName);
|
const char *nn_methodDoc(nn_componentTable *table, const char *methodName);
|
||||||
|
|
||||||
// Component calling
|
// Component calling
|
||||||
@ -482,52 +503,52 @@ void nn_simulateBufferedIndirect(nn_component *component, double amount, double
|
|||||||
void nn_resetCall(nn_computer *computer);
|
void nn_resetCall(nn_computer *computer);
|
||||||
void nn_addArgument(nn_computer *computer, nn_value arg);
|
void nn_addArgument(nn_computer *computer, nn_value arg);
|
||||||
void nn_return(nn_computer *computer, nn_value val);
|
void nn_return(nn_computer *computer, nn_value val);
|
||||||
nn_value nn_getArgument(nn_computer *computer, size_t idx);
|
nn_value nn_getArgument(nn_computer *computer, nn_size_t idx);
|
||||||
nn_value nn_getReturn(nn_computer *computer, size_t idx);
|
nn_value nn_getReturn(nn_computer *computer, nn_size_t idx);
|
||||||
size_t nn_getArgumentCount(nn_computer *computer);
|
nn_size_t nn_getArgumentCount(nn_computer *computer);
|
||||||
size_t nn_getReturnCount(nn_computer *computer);
|
nn_size_t nn_getReturnCount(nn_computer *computer);
|
||||||
|
|
||||||
// Value stuff
|
// Value stuff
|
||||||
|
|
||||||
nn_value nn_values_nil();
|
nn_value nn_values_nil();
|
||||||
nn_value nn_values_integer(intptr_t integer);
|
nn_value nn_values_integer(nn_intptr_t integer);
|
||||||
nn_value nn_values_number(double num);
|
nn_value nn_values_number(double num);
|
||||||
nn_value nn_values_boolean(nn_bool_t boolean);
|
nn_value nn_values_boolean(nn_bool_t boolean);
|
||||||
nn_value nn_values_cstring(const char *string);
|
nn_value nn_values_cstring(const char *string);
|
||||||
nn_value nn_values_string(nn_Alloc *alloc, const char *string, size_t len);
|
nn_value nn_values_string(nn_Alloc *alloc, const char *string, nn_size_t len);
|
||||||
nn_value nn_values_array(nn_Alloc *alloc, size_t len);
|
nn_value nn_values_array(nn_Alloc *alloc, nn_size_t len);
|
||||||
nn_value nn_values_table(nn_Alloc *alloc, size_t pairCount);
|
nn_value nn_values_table(nn_Alloc *alloc, nn_size_t pairCount);
|
||||||
|
|
||||||
void nn_return_nil(nn_computer *computer);
|
void nn_return_nil(nn_computer *computer);
|
||||||
void nn_return_integer(nn_computer *computer, intptr_t integer);
|
void nn_return_integer(nn_computer *computer, nn_intptr_t integer);
|
||||||
void nn_return_number(nn_computer *computer, double number);
|
void nn_return_number(nn_computer *computer, double number);
|
||||||
void nn_return_boolean(nn_computer *computer, nn_bool_t boolean);
|
void nn_return_boolean(nn_computer *computer, nn_bool_t boolean);
|
||||||
void nn_return_cstring(nn_computer *computer, const char *cstr);
|
void nn_return_cstring(nn_computer *computer, const char *cstr);
|
||||||
void nn_return_string(nn_computer *computer, const char *str, size_t len);
|
void nn_return_string(nn_computer *computer, const char *str, nn_size_t len);
|
||||||
nn_value nn_return_array(nn_computer *computer, size_t len);
|
nn_value nn_return_array(nn_computer *computer, nn_size_t len);
|
||||||
nn_value nn_return_table(nn_computer *computer, size_t len);
|
nn_value nn_return_table(nn_computer *computer, nn_size_t len);
|
||||||
|
|
||||||
size_t nn_values_getType(nn_value val);
|
nn_size_t nn_values_getType(nn_value val);
|
||||||
nn_value nn_values_retain(nn_value val);
|
nn_value nn_values_retain(nn_value val);
|
||||||
void nn_values_drop(nn_value val);
|
void nn_values_drop(nn_value val);
|
||||||
|
|
||||||
void nn_values_set(nn_value arr, size_t idx, nn_value val);
|
void nn_values_set(nn_value arr, nn_size_t idx, nn_value val);
|
||||||
nn_value nn_values_get(nn_value arr, size_t idx);
|
nn_value nn_values_get(nn_value arr, nn_size_t idx);
|
||||||
|
|
||||||
void nn_values_setPair(nn_value obj, size_t idx, nn_value key, nn_value val);
|
void nn_values_setPair(nn_value obj, nn_size_t idx, nn_value key, nn_value val);
|
||||||
nn_pair nn_values_getPair(nn_value obj, size_t idx);
|
nn_pair nn_values_getPair(nn_value obj, nn_size_t idx);
|
||||||
|
|
||||||
intptr_t nn_toInt(nn_value val);
|
nn_intptr_t nn_toInt(nn_value val);
|
||||||
double nn_toNumber(nn_value val);
|
double nn_toNumber(nn_value val);
|
||||||
nn_bool_t nn_toBoolean(nn_value val);
|
nn_bool_t nn_toBoolean(nn_value val);
|
||||||
const char *nn_toCString(nn_value val);
|
const char *nn_toCString(nn_value val);
|
||||||
const char *nn_toString(nn_value val, size_t *len);
|
const char *nn_toString(nn_value val, nn_size_t *len);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Computes the "packet size" of the values, using the same algorithm as OC.
|
* Computes the "packet size" of the values, using the same algorithm as OC.
|
||||||
* This is used by pushSignal to check the size
|
* This is used by pushSignal to check the size
|
||||||
*/
|
*/
|
||||||
size_t nn_measurePacketSize(nn_value *vals, size_t len);
|
nn_size_t nn_measurePacketSize(nn_value *vals, nn_size_t len);
|
||||||
|
|
||||||
// COMPONENTS
|
// COMPONENTS
|
||||||
|
|
||||||
@ -566,14 +587,14 @@ typedef struct nn_eeprom {
|
|||||||
nn_eepromControl (*control)(nn_component *component, void *userdata);
|
nn_eepromControl (*control)(nn_component *component, void *userdata);
|
||||||
|
|
||||||
// methods
|
// methods
|
||||||
size_t (*getSize)(nn_component *component, void *userdata);
|
nn_size_t (*getSize)(nn_component *component, void *userdata);
|
||||||
size_t (*getDataSize)(nn_component *component, void *userdata);
|
nn_size_t (*getDataSize)(nn_component *component, void *userdata);
|
||||||
void (*getLabel)(nn_component *component, void *userdata, char *buf, size_t *buflen);
|
void (*getLabel)(nn_component *component, void *userdata, char *buf, nn_size_t *buflen);
|
||||||
size_t (*setLabel)(nn_component *component, void *userdata, const char *buf, size_t buflen);
|
nn_size_t (*setLabel)(nn_component *component, void *userdata, const char *buf, nn_size_t buflen);
|
||||||
size_t (*get)(nn_component *component, void *userdata, char *buf);
|
nn_size_t (*get)(nn_component *component, void *userdata, char *buf);
|
||||||
void (*set)(nn_component *component, void *userdata, const char *buf, size_t len);
|
void (*set)(nn_component *component, void *userdata, const char *buf, nn_size_t len);
|
||||||
int (*getData)(nn_component *component, void *userdata, char *buf);
|
int (*getData)(nn_component *component, void *userdata, char *buf);
|
||||||
void (*setData)(nn_component *component, void *userdata, const char *buf, size_t len);
|
void (*setData)(nn_component *component, void *userdata, const char *buf, nn_size_t len);
|
||||||
nn_bool_t (*isReadonly)(nn_component *component, void *userdata);
|
nn_bool_t (*isReadonly)(nn_component *component, void *userdata);
|
||||||
void (*makeReadonly)(nn_component *component, void *userdata);
|
void (*makeReadonly)(nn_component *component, void *userdata);
|
||||||
} nn_eeprom;
|
} nn_eeprom;
|
||||||
@ -601,9 +622,9 @@ typedef struct nn_filesystemControl {
|
|||||||
double writeHeatPerChunk;
|
double writeHeatPerChunk;
|
||||||
|
|
||||||
// call budget
|
// call budget
|
||||||
size_t readCostPerChunk;
|
nn_size_t readCostPerChunk;
|
||||||
size_t writeCostPerChunk;
|
nn_size_t writeCostPerChunk;
|
||||||
size_t seekCostPerChunk;
|
nn_size_t seekCostPerChunk;
|
||||||
|
|
||||||
// energy cost
|
// energy cost
|
||||||
double readEnergyCost;
|
double readEnergyCost;
|
||||||
@ -617,18 +638,18 @@ typedef struct nn_filesystem {
|
|||||||
void (*deinit)(nn_component *component, void *userdata);
|
void (*deinit)(nn_component *component, void *userdata);
|
||||||
|
|
||||||
nn_filesystemControl (*control)(nn_component *component, void *userdata);
|
nn_filesystemControl (*control)(nn_component *component, void *userdata);
|
||||||
void (*getLabel)(nn_component *component, void *userdata, char *buf, size_t *buflen);
|
void (*getLabel)(nn_component *component, void *userdata, char *buf, nn_size_t *buflen);
|
||||||
size_t (*setLabel)(nn_component *component, void *userdata, const char *buf, size_t buflen);
|
nn_size_t (*setLabel)(nn_component *component, void *userdata, const char *buf, nn_size_t buflen);
|
||||||
|
|
||||||
size_t (*spaceUsed)(nn_component *component, void *userdata);
|
nn_size_t (*spaceUsed)(nn_component *component, void *userdata);
|
||||||
size_t (*spaceTotal)(nn_component *component, void *userdata);
|
nn_size_t (*spaceTotal)(nn_component *component, void *userdata);
|
||||||
nn_bool_t (*isReadOnly)(nn_component *component, void *userdata);
|
nn_bool_t (*isReadOnly)(nn_component *component, void *userdata);
|
||||||
|
|
||||||
// general operations
|
// general operations
|
||||||
size_t (*size)(nn_component *component, void *userdata, const char *path);
|
nn_size_t (*size)(nn_component *component, void *userdata, const char *path);
|
||||||
nn_bool_t (*remove)(nn_component *component, void *userdata, const char *path);
|
nn_bool_t (*remove)(nn_component *component, void *userdata, const char *path);
|
||||||
size_t (*lastModified)(nn_component *component, void *userdata, const char *path);
|
nn_size_t (*lastModified)(nn_component *component, void *userdata, const char *path);
|
||||||
size_t (*rename)(nn_component *component, void *userdata, const char *from, const char *to);
|
nn_size_t (*rename)(nn_component *component, void *userdata, const char *from, const char *to);
|
||||||
nn_bool_t (*exists)(nn_component *component, void *userdata, const char *path);
|
nn_bool_t (*exists)(nn_component *component, void *userdata, const char *path);
|
||||||
|
|
||||||
// directory operations
|
// directory operations
|
||||||
@ -640,18 +661,18 @@ typedef struct nn_filesystem {
|
|||||||
// If it is not, the behavior is undefined.
|
// If it is not, the behavior is undefined.
|
||||||
// We recommend first computing len then allocating, though if that is not doable or practical,
|
// We recommend first computing len then allocating, though if that is not doable or practical,
|
||||||
// consider nn_resize()ing it to the correct size to guarantee a correct deallocation.
|
// consider nn_resize()ing it to the correct size to guarantee a correct deallocation.
|
||||||
char **(*list)(nn_Alloc *alloc, nn_component *component, void *userdata, const char *path, size_t *len);
|
char **(*list)(nn_Alloc *alloc, nn_component *component, void *userdata, const char *path, nn_size_t *len);
|
||||||
|
|
||||||
// file operations
|
// file operations
|
||||||
size_t (*open)(nn_component *component, void *userdata, const char *path, const char *mode);
|
nn_size_t (*open)(nn_component *component, void *userdata, const char *path, const char *mode);
|
||||||
nn_bool_t (*close)(nn_component *component, void *userdata, int fd);
|
nn_bool_t (*close)(nn_component *component, void *userdata, int fd);
|
||||||
nn_bool_t (*write)(nn_component *component, void *userdata, int fd, const char *buf, size_t len);
|
nn_bool_t (*write)(nn_component *component, void *userdata, int fd, const char *buf, nn_size_t len);
|
||||||
size_t (*read)(nn_component *component, void *userdata, int fd, char *buf, size_t required);
|
nn_size_t (*read)(nn_component *component, void *userdata, int fd, char *buf, nn_size_t required);
|
||||||
// moved is an out pointer that says how many bytes the pointer moved.
|
// moved is an out pointer that says how many bytes the pointer moved.
|
||||||
size_t (*seek)(nn_component *component, void *userdata, int fd, const char *whence, int off, int *moved);
|
nn_size_t (*seek)(nn_component *component, void *userdata, int fd, const char *whence, int off, int *moved);
|
||||||
} nn_filesystem;
|
} nn_filesystem;
|
||||||
|
|
||||||
nn_filesystem *nn_volatileFileSystem(size_t capacity, nn_filesystemControl *control);
|
nn_filesystem *nn_volatileFileSystem(nn_size_t capacity, nn_filesystemControl *control);
|
||||||
nn_component *nn_addFileSystem(nn_computer *computer, nn_address address, int slot, nn_filesystem *filesystem);
|
nn_component *nn_addFileSystem(nn_computer *computer, nn_address address, int slot, nn_filesystem *filesystem);
|
||||||
|
|
||||||
// Drive
|
// Drive
|
||||||
@ -675,9 +696,9 @@ typedef struct nn_driveControl {
|
|||||||
double writeEnergyCost;
|
double writeEnergyCost;
|
||||||
|
|
||||||
// call budget
|
// call budget
|
||||||
size_t readCostPerSector;
|
nn_size_t readCostPerSector;
|
||||||
size_t writeCostPerSector;
|
nn_size_t writeCostPerSector;
|
||||||
size_t seekCostPerSector;
|
nn_size_t seekCostPerSector;
|
||||||
} nn_driveControl;
|
} nn_driveControl;
|
||||||
|
|
||||||
typedef struct nn_drive {
|
typedef struct nn_drive {
|
||||||
@ -686,12 +707,12 @@ typedef struct nn_drive {
|
|||||||
void (*deinit)(nn_component *component, void *userdata);
|
void (*deinit)(nn_component *component, void *userdata);
|
||||||
|
|
||||||
nn_driveControl (*control)(nn_component *component, void *userdata);
|
nn_driveControl (*control)(nn_component *component, void *userdata);
|
||||||
void (*getLabel)(nn_component *component, void *userdata, char *buf, size_t *buflen);
|
void (*getLabel)(nn_component *component, void *userdata, char *buf, nn_size_t *buflen);
|
||||||
size_t (*setLabel)(nn_component *component, void *userdata, const char *buf, size_t buflen);
|
nn_size_t (*setLabel)(nn_component *component, void *userdata, const char *buf, nn_size_t buflen);
|
||||||
|
|
||||||
size_t (*getPlatterCount)(nn_component *component, void *userdata);
|
nn_size_t (*getPlatterCount)(nn_component *component, void *userdata);
|
||||||
size_t (*getCapacity)(nn_component *component, void *userdata);
|
nn_size_t (*getCapacity)(nn_component *component, void *userdata);
|
||||||
size_t (*getSectorSize)(nn_component *component, void *userdata);
|
nn_size_t (*getSectorSize)(nn_component *component, void *userdata);
|
||||||
|
|
||||||
// sectors start at 1 as per OC.
|
// sectors start at 1 as per OC.
|
||||||
void (*readSector)(nn_component *component, void *userdata, int sector, char *buf);
|
void (*readSector)(nn_component *component, void *userdata, int sector, char *buf);
|
||||||
@ -701,7 +722,7 @@ typedef struct nn_drive {
|
|||||||
// Also makes the interface less redundant
|
// Also makes the interface less redundant
|
||||||
} nn_drive;
|
} nn_drive;
|
||||||
|
|
||||||
nn_drive *nn_volatileDrive(size_t capacity, size_t platterCount, nn_driveControl *control);
|
nn_drive *nn_volatileDrive(nn_size_t capacity, nn_size_t platterCount, nn_driveControl *control);
|
||||||
nn_component *nn_addDrive(nn_computer *computer, nn_address address, int slot, nn_drive *drive);
|
nn_component *nn_addDrive(nn_computer *computer, nn_address address, int slot, nn_drive *drive);
|
||||||
|
|
||||||
// Screens and GPUs
|
// Screens and GPUs
|
||||||
@ -736,8 +757,8 @@ void nn_setAspectRatio(nn_screen *screen, int width, int height);
|
|||||||
|
|
||||||
void nn_addKeyboard(nn_screen *screen, nn_address address);
|
void nn_addKeyboard(nn_screen *screen, nn_address address);
|
||||||
void nn_removeKeyboard(nn_screen *screen, nn_address address);
|
void nn_removeKeyboard(nn_screen *screen, nn_address address);
|
||||||
nn_address nn_getKeyboard(nn_screen *screen, size_t idx);
|
nn_address nn_getKeyboard(nn_screen *screen, nn_size_t idx);
|
||||||
size_t nn_getKeyboardCount(nn_screen *screen);
|
nn_size_t nn_getKeyboardCount(nn_screen *screen);
|
||||||
|
|
||||||
void nn_setEditableColors(nn_screen *screen, int count);
|
void nn_setEditableColors(nn_screen *screen, int count);
|
||||||
int nn_getEditableColors(nn_screen *screen);
|
int nn_getEditableColors(nn_screen *screen);
|
||||||
|
@ -202,9 +202,9 @@ nn_bool_t nn_unicode_validate(const char *b) {
|
|||||||
// It is used to power the Lua architecture's Unicode API re-implementation.
|
// It is used to power the Lua architecture's Unicode API re-implementation.
|
||||||
// It can also just be used to deal with unicode.
|
// It can also just be used to deal with unicode.
|
||||||
|
|
||||||
char *nn_unicode_char(nn_Alloc *alloc, unsigned int *codepoints, size_t codepointCount) {
|
char *nn_unicode_char(nn_Alloc *alloc, unsigned int *codepoints, nn_size_t codepointCount) {
|
||||||
size_t len = 0;
|
nn_size_t len = 0;
|
||||||
for (size_t i = 0; i < codepointCount; i++) {
|
for (nn_size_t i = 0; i < codepointCount; i++) {
|
||||||
unsigned int codepoint = codepoints[i];
|
unsigned int codepoint = codepoints[i];
|
||||||
len += nn_unicode_codepointSize(codepoint);
|
len += nn_unicode_codepointSize(codepoint);
|
||||||
}
|
}
|
||||||
@ -212,10 +212,10 @@ char *nn_unicode_char(nn_Alloc *alloc, unsigned int *codepoints, size_t codepoin
|
|||||||
char *buf = nn_alloc(alloc, len+1);
|
char *buf = nn_alloc(alloc, len+1);
|
||||||
if (buf == NULL) return buf;
|
if (buf == NULL) return buf;
|
||||||
|
|
||||||
size_t j = 0;
|
nn_size_t j = 0;
|
||||||
for (size_t i = 0; i < codepointCount; i++) {
|
for (nn_size_t i = 0; i < codepointCount; i++) {
|
||||||
int codepoint = codepoints[i];
|
int codepoint = codepoints[i];
|
||||||
size_t codepointLen = 0;
|
nn_size_t codepointLen = 0;
|
||||||
char c[NN_MAXIMUM_UNICODE_BUFFER];
|
char c[NN_MAXIMUM_UNICODE_BUFFER];
|
||||||
nn_unicode_codepointToChar(c, codepoint, &codepointLen);
|
nn_unicode_codepointToChar(c, codepoint, &codepointLen);
|
||||||
nn_memcpy(buf + j, c, codepointLen);
|
nn_memcpy(buf + j, c, codepointLen);
|
||||||
@ -226,13 +226,13 @@ char *nn_unicode_char(nn_Alloc *alloc, unsigned int *codepoints, size_t codepoin
|
|||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int *nn_unicode_codepoints(nn_Alloc *alloc, const char *s, size_t *len) {
|
unsigned int *nn_unicode_codepoints(nn_Alloc *alloc, const char *s, nn_size_t *len) {
|
||||||
size_t l = nn_unicode_len(s);
|
nn_size_t l = nn_unicode_len(s);
|
||||||
unsigned int *buf = nn_alloc(alloc, sizeof(unsigned int) * l);
|
unsigned int *buf = nn_alloc(alloc, sizeof(unsigned int) * l);
|
||||||
if(buf == NULL) return NULL;
|
if(buf == NULL) return NULL;
|
||||||
if(len != NULL) *len = l;
|
if(len != NULL) *len = l;
|
||||||
size_t cur = 0;
|
nn_size_t cur = 0;
|
||||||
size_t bufidx = 0;
|
nn_size_t bufidx = 0;
|
||||||
while(s[cur] != 0) {
|
while(s[cur] != 0) {
|
||||||
unsigned int point = nn_unicode_codepointAt(s, cur);
|
unsigned int point = nn_unicode_codepointAt(s, cur);
|
||||||
cur += nn_unicode_codepointSize(point);
|
cur += nn_unicode_codepointSize(point);
|
||||||
@ -241,8 +241,8 @@ unsigned int *nn_unicode_codepoints(nn_Alloc *alloc, const char *s, size_t *len)
|
|||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t nn_unicode_len(const char *b) {
|
nn_size_t nn_unicode_len(const char *b) {
|
||||||
size_t count = 0;
|
nn_size_t count = 0;
|
||||||
const unsigned char* s = (const unsigned char*)b;
|
const unsigned char* s = (const unsigned char*)b;
|
||||||
while (*s) {
|
while (*s) {
|
||||||
count++;
|
count++;
|
||||||
@ -259,7 +259,7 @@ size_t nn_unicode_len(const char *b) {
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int nn_unicode_codepointAt(const char *s, size_t byteOffset) {
|
unsigned int nn_unicode_codepointAt(const char *s, nn_size_t byteOffset) {
|
||||||
unsigned int point = 0;
|
unsigned int point = 0;
|
||||||
const unsigned char *b = (const unsigned char *)s + byteOffset;
|
const unsigned char *b = (const unsigned char *)s + byteOffset;
|
||||||
|
|
||||||
@ -283,7 +283,7 @@ unsigned int nn_unicode_codepointAt(const char *s, size_t byteOffset) {
|
|||||||
return point;
|
return point;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t nn_unicode_codepointSize(unsigned int codepoint) {
|
nn_size_t nn_unicode_codepointSize(unsigned int codepoint) {
|
||||||
if (codepoint <= 0x007f) {
|
if (codepoint <= 0x007f) {
|
||||||
return 1;
|
return 1;
|
||||||
} else if (codepoint <= 0x07ff) {
|
} else if (codepoint <= 0x07ff) {
|
||||||
@ -297,8 +297,8 @@ size_t nn_unicode_codepointSize(unsigned int codepoint) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void nn_unicode_codepointToChar(char *buffer, unsigned int codepoint, size_t *len) {
|
void nn_unicode_codepointToChar(char *buffer, unsigned int codepoint, nn_size_t *len) {
|
||||||
size_t codepointSize = nn_unicode_codepointSize(codepoint);
|
nn_size_t codepointSize = nn_unicode_codepointSize(codepoint);
|
||||||
*len = codepointSize;
|
*len = codepointSize;
|
||||||
|
|
||||||
nn_memset(buffer, 0, 4); // Clear static array
|
nn_memset(buffer, 0, 4); // Clear static array
|
||||||
@ -323,7 +323,7 @@ void nn_unicode_codepointToChar(char *buffer, unsigned int codepoint, size_t *le
|
|||||||
// copied straight from opencomputers and musl's libc
|
// copied straight from opencomputers and musl's libc
|
||||||
// https://github.com/MightyPirates/OpenComputers/blob/52da41b5e171b43fea80342dc75d808f97a0f797/src/main/scala/li/cil/oc/util/FontUtils.scala#L205
|
// https://github.com/MightyPirates/OpenComputers/blob/52da41b5e171b43fea80342dc75d808f97a0f797/src/main/scala/li/cil/oc/util/FontUtils.scala#L205
|
||||||
// https://git.musl-libc.org/cgit/musl/tree/src/ctype/wcwidth.c
|
// https://git.musl-libc.org/cgit/musl/tree/src/ctype/wcwidth.c
|
||||||
size_t nn_unicode_charWidth(unsigned int codepoint) {
|
nn_size_t nn_unicode_charWidth(unsigned int codepoint) {
|
||||||
if (codepoint < 0xff) {
|
if (codepoint < 0xff) {
|
||||||
if (((codepoint + 1) & 0x7f) >= 0x21) {
|
if (((codepoint + 1) & 0x7f) >= 0x21) {
|
||||||
return 1;
|
return 1;
|
||||||
@ -344,11 +344,11 @@ size_t nn_unicode_charWidth(unsigned int codepoint) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t nn_unicode_wlen(const char *s) {
|
nn_size_t nn_unicode_wlen(const char *s) {
|
||||||
size_t wlen = 0;
|
nn_size_t wlen = 0;
|
||||||
while (*s) {
|
while (*s) {
|
||||||
unsigned int codepoint = nn_unicode_codepointAt(s, 0);
|
unsigned int codepoint = nn_unicode_codepointAt(s, 0);
|
||||||
size_t codepointSize = nn_unicode_codepointSize(codepoint);
|
nn_size_t codepointSize = nn_unicode_codepointSize(codepoint);
|
||||||
wlen += nn_unicode_charWidth(codepoint);
|
wlen += nn_unicode_charWidth(codepoint);
|
||||||
s += codepointSize;
|
s += codepointSize;
|
||||||
}
|
}
|
||||||
|
@ -15,14 +15,14 @@ nn_Alloc *nn_getAllocator(nn_universe *universe) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void nn_unsafeDeleteUniverse(nn_universe *universe) {
|
void nn_unsafeDeleteUniverse(nn_universe *universe) {
|
||||||
for(size_t i = 0; i < universe->udataLen; i++) {
|
for(nn_size_t i = 0; i < universe->udataLen; i++) {
|
||||||
nn_deallocStr(&universe->ctx.allocator, universe->udata[i].name);
|
nn_deallocStr(&universe->ctx.allocator, universe->udata[i].name);
|
||||||
}
|
}
|
||||||
nn_dealloc(&universe->ctx.allocator, universe, sizeof(nn_universe));
|
nn_dealloc(&universe->ctx.allocator, universe, sizeof(nn_universe));
|
||||||
}
|
}
|
||||||
|
|
||||||
void *nn_queryUserdata(nn_universe *universe, const char *name) {
|
void *nn_queryUserdata(nn_universe *universe, const char *name) {
|
||||||
for(size_t i = 0; i < universe->udataLen; i++) {
|
for(nn_size_t i = 0; i < universe->udataLen; i++) {
|
||||||
if(nn_strcmp(universe->udata[i].name, name) == 0) {
|
if(nn_strcmp(universe->udata[i].name, name) == 0) {
|
||||||
return universe->udata[i].userdata;
|
return universe->udata[i].userdata;
|
||||||
}
|
}
|
||||||
@ -33,7 +33,7 @@ void *nn_queryUserdata(nn_universe *universe, const char *name) {
|
|||||||
void nn_storeUserdata(nn_universe *universe, const char *name, void *data) {
|
void nn_storeUserdata(nn_universe *universe, const char *name, void *data) {
|
||||||
if(universe->udataLen == NN_MAX_USERDATA) return; // prevent overflow
|
if(universe->udataLen == NN_MAX_USERDATA) return; // prevent overflow
|
||||||
|
|
||||||
size_t idx = universe->udataLen;
|
nn_size_t idx = universe->udataLen;
|
||||||
char *allocName = nn_strdup(&universe->ctx.allocator, name);
|
char *allocName = nn_strdup(&universe->ctx.allocator, name);
|
||||||
if(allocName == NULL) return;
|
if(allocName == NULL) return;
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ typedef struct nn_universe_udata {
|
|||||||
typedef struct nn_universe {
|
typedef struct nn_universe {
|
||||||
nn_Context ctx;
|
nn_Context ctx;
|
||||||
nn_universe_udata udata[NN_MAX_USERDATA];
|
nn_universe_udata udata[NN_MAX_USERDATA];
|
||||||
size_t udataLen;
|
nn_size_t udataLen;
|
||||||
} nn_universe;
|
} nn_universe;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
34
src/utils.c
34
src/utils.c
@ -10,12 +10,12 @@
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void *nn_alloc(nn_Alloc *alloc, size_t size) {
|
void *nn_alloc(nn_Alloc *alloc, nn_size_t size) {
|
||||||
if(size == 0) return alloc->proc;
|
if(size == 0) return alloc->proc;
|
||||||
return alloc->proc(alloc->userdata, NULL, 0, size, NULL);
|
return alloc->proc(alloc->userdata, NULL, 0, size, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *nn_resize(nn_Alloc *alloc, void *memory, size_t oldSize, size_t newSize) {
|
void *nn_resize(nn_Alloc *alloc, void *memory, nn_size_t oldSize, nn_size_t newSize) {
|
||||||
if(oldSize == newSize) return memory;
|
if(oldSize == newSize) return memory;
|
||||||
if(newSize == 0) {
|
if(newSize == 0) {
|
||||||
nn_dealloc(alloc, memory, oldSize);
|
nn_dealloc(alloc, memory, oldSize);
|
||||||
@ -31,7 +31,7 @@ void *nn_resize(nn_Alloc *alloc, void *memory, size_t oldSize, size_t newSize) {
|
|||||||
return alloc->proc(alloc->userdata, memory, oldSize, newSize, NULL);
|
return alloc->proc(alloc->userdata, memory, oldSize, newSize, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void nn_dealloc(nn_Alloc *alloc, void *memory, size_t size) {
|
void nn_dealloc(nn_Alloc *alloc, void *memory, nn_size_t size) {
|
||||||
if(memory == NULL) return; // matches free()
|
if(memory == NULL) return; // matches free()
|
||||||
if(memory == alloc->proc) return; // 0-sized memory
|
if(memory == alloc->proc) return; // 0-sized memory
|
||||||
alloc->proc(alloc->userdata, memory, size, 0, NULL);
|
alloc->proc(alloc->userdata, memory, size, 0, NULL);
|
||||||
@ -41,7 +41,7 @@ void nn_dealloc(nn_Alloc *alloc, void *memory, size_t size) {
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
static void *nn_libcAllocProc(void *_, void *ptr, size_t oldSize, size_t newSize, void *__) {
|
static void *nn_libcAllocProc(void *_, void *ptr, nn_size_t oldSize, nn_size_t newSize, void *__) {
|
||||||
if(newSize == 0) {
|
if(newSize == 0) {
|
||||||
//printf("Freed %lu bytes from %p\n", oldSize, ptr);
|
//printf("Freed %lu bytes from %p\n", oldSize, ptr);
|
||||||
free(ptr);
|
free(ptr);
|
||||||
@ -60,7 +60,7 @@ nn_Alloc nn_libcAllocator() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t nni_rand(void *userdata) {
|
static nn_size_t nni_rand(void *userdata) {
|
||||||
return rand();
|
return rand();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,13 +85,13 @@ nn_Context nn_libcContext() {
|
|||||||
|
|
||||||
// Utilities, both internal and external
|
// Utilities, both internal and external
|
||||||
char *nn_strdup(nn_Alloc *alloc, const char *s) {
|
char *nn_strdup(nn_Alloc *alloc, const char *s) {
|
||||||
size_t l = nn_strlen(s);
|
nn_size_t l = nn_strlen(s);
|
||||||
char *m = nn_alloc(alloc, l+1);
|
char *m = nn_alloc(alloc, l+1);
|
||||||
if(m == NULL) return m;
|
if(m == NULL) return m;
|
||||||
return nn_strcpy(m, s);
|
return nn_strcpy(m, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *nn_memdup(nn_Alloc *alloc, const void *buf, size_t len) {
|
void *nn_memdup(nn_Alloc *alloc, const void *buf, nn_size_t len) {
|
||||||
char *m = nn_alloc(alloc, len);
|
char *m = nn_alloc(alloc, len);
|
||||||
if(m == NULL) return m;
|
if(m == NULL) return m;
|
||||||
nn_memcpy(m, buf, len);
|
nn_memcpy(m, buf, len);
|
||||||
@ -103,7 +103,7 @@ void nn_deallocStr(nn_Alloc *alloc, char *s) {
|
|||||||
nn_dealloc(alloc, s, nn_strlen(s)+1);
|
nn_dealloc(alloc, s, nn_strlen(s)+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t nn_rand(nn_Rng *rng) {
|
nn_size_t nn_rand(nn_Rng *rng) {
|
||||||
return rng->proc(rng->userdata);
|
return rng->proc(rng->userdata);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,21 +205,21 @@ int nn_mapColor(int color, int *palette, int paletteSize) {
|
|||||||
return bestColor;
|
return bestColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
void nn_memset(void *buf, unsigned char byte, size_t len) {
|
void nn_memset(void *buf, unsigned char byte, nn_size_t len) {
|
||||||
unsigned char *bytes = buf;
|
unsigned char *bytes = buf;
|
||||||
for(size_t i = 0; i < len; i++) bytes[i] = byte;
|
for(nn_size_t i = 0; i < len; i++) bytes[i] = byte;
|
||||||
}
|
}
|
||||||
|
|
||||||
void nn_memcpy(void *dest, const void *src, size_t len) {
|
void nn_memcpy(void *dest, const void *src, nn_size_t len) {
|
||||||
char *destBytes = dest;
|
char *destBytes = dest;
|
||||||
const char *srcBytes = src;
|
const char *srcBytes = src;
|
||||||
for(size_t i = 0; i < len; i++) {
|
for(nn_size_t i = 0; i < len; i++) {
|
||||||
destBytes[i] = srcBytes[i];
|
destBytes[i] = srcBytes[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
char *nn_strcpy(char *dest, const char *src) {
|
char *nn_strcpy(char *dest, const char *src) {
|
||||||
size_t i = 0;
|
nn_size_t i = 0;
|
||||||
while(src[i]) {
|
while(src[i]) {
|
||||||
dest[i] = src[i];
|
dest[i] = src[i];
|
||||||
i++;
|
i++;
|
||||||
@ -229,7 +229,7 @@ char *nn_strcpy(char *dest, const char *src) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int nn_strcmp(const char *a, const char *b) {
|
int nn_strcmp(const char *a, const char *b) {
|
||||||
size_t i = 0;
|
nn_size_t i = 0;
|
||||||
while(NN_TRUE) {
|
while(NN_TRUE) {
|
||||||
unsigned char ca = a[i];
|
unsigned char ca = a[i];
|
||||||
unsigned char cb = b[i];
|
unsigned char cb = b[i];
|
||||||
@ -248,7 +248,7 @@ int nn_strcmp(const char *a, const char *b) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const char *nn_strchr(const char *str, int ch) {
|
const char *nn_strchr(const char *str, int ch) {
|
||||||
size_t i = 0;
|
nn_size_t i = 0;
|
||||||
while(NN_TRUE) {
|
while(NN_TRUE) {
|
||||||
if(str[i] == ch) return str + i;
|
if(str[i] == ch) return str + i;
|
||||||
if(str[i] == 0) return NULL;
|
if(str[i] == 0) return NULL;
|
||||||
@ -256,8 +256,8 @@ const char *nn_strchr(const char *str, int ch) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t nn_strlen(const char *a) {
|
nn_size_t nn_strlen(const char *a) {
|
||||||
size_t l = 0;
|
nn_size_t l = 0;
|
||||||
while(a[l]) l++;
|
while(a[l]) l++;
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
46
src/value.c
46
src/value.c
@ -4,7 +4,7 @@ nn_value nn_values_nil() {
|
|||||||
return (nn_value) {.tag = NN_VALUE_NIL};
|
return (nn_value) {.tag = NN_VALUE_NIL};
|
||||||
}
|
}
|
||||||
|
|
||||||
nn_value nn_values_integer(intptr_t integer) {
|
nn_value nn_values_integer(nn_intptr_t integer) {
|
||||||
return (nn_value) {.tag = NN_VALUE_INT, .integer = integer};
|
return (nn_value) {.tag = NN_VALUE_INT, .integer = integer};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -20,7 +20,7 @@ nn_value nn_values_cstring(const char *string) {
|
|||||||
return (nn_value) {.tag = NN_VALUE_CSTR, .cstring = string};
|
return (nn_value) {.tag = NN_VALUE_CSTR, .cstring = string};
|
||||||
}
|
}
|
||||||
|
|
||||||
nn_value nn_values_string(nn_Alloc *alloc, const char *string, size_t len) {
|
nn_value nn_values_string(nn_Alloc *alloc, const char *string, nn_size_t len) {
|
||||||
char *buf = nn_alloc(alloc, len+1);
|
char *buf = nn_alloc(alloc, len+1);
|
||||||
if(buf == NULL) {
|
if(buf == NULL) {
|
||||||
return nn_values_nil();
|
return nn_values_nil();
|
||||||
@ -41,7 +41,7 @@ nn_value nn_values_string(nn_Alloc *alloc, const char *string, size_t len) {
|
|||||||
return (nn_value) {.tag = NN_VALUE_STR, .string = s};
|
return (nn_value) {.tag = NN_VALUE_STR, .string = s};
|
||||||
}
|
}
|
||||||
|
|
||||||
nn_value nn_values_array(nn_Alloc *alloc, size_t len) {
|
nn_value nn_values_array(nn_Alloc *alloc, nn_size_t len) {
|
||||||
nn_array *arr = nn_alloc(alloc, sizeof(nn_array));
|
nn_array *arr = nn_alloc(alloc, sizeof(nn_array));
|
||||||
if(arr == NULL) {
|
if(arr == NULL) {
|
||||||
return nn_values_nil();
|
return nn_values_nil();
|
||||||
@ -54,14 +54,14 @@ nn_value nn_values_array(nn_Alloc *alloc, size_t len) {
|
|||||||
nn_dealloc(alloc, arr, sizeof(nn_array));
|
nn_dealloc(alloc, arr, sizeof(nn_array));
|
||||||
return nn_values_nil();
|
return nn_values_nil();
|
||||||
}
|
}
|
||||||
for(size_t i = 0; i < len; i++) {
|
for(nn_size_t i = 0; i < len; i++) {
|
||||||
values[i] = nn_values_nil();
|
values[i] = nn_values_nil();
|
||||||
}
|
}
|
||||||
arr->values = values;
|
arr->values = values;
|
||||||
return (nn_value) {.tag = NN_VALUE_ARRAY, .array = arr};
|
return (nn_value) {.tag = NN_VALUE_ARRAY, .array = arr};
|
||||||
}
|
}
|
||||||
|
|
||||||
nn_value nn_values_table(nn_Alloc *alloc, size_t pairCount) {
|
nn_value nn_values_table(nn_Alloc *alloc, nn_size_t pairCount) {
|
||||||
nn_table *table = nn_alloc(alloc, sizeof(nn_table));
|
nn_table *table = nn_alloc(alloc, sizeof(nn_table));
|
||||||
if(table == NULL) {
|
if(table == NULL) {
|
||||||
return nn_values_nil();
|
return nn_values_nil();
|
||||||
@ -74,7 +74,7 @@ nn_value nn_values_table(nn_Alloc *alloc, size_t pairCount) {
|
|||||||
nn_dealloc(alloc, table, sizeof(nn_table));
|
nn_dealloc(alloc, table, sizeof(nn_table));
|
||||||
return nn_values_nil();
|
return nn_values_nil();
|
||||||
}
|
}
|
||||||
for(size_t i = 0; i < pairCount; i++) {
|
for(nn_size_t i = 0; i < pairCount; i++) {
|
||||||
pairs[i].key = nn_values_nil();
|
pairs[i].key = nn_values_nil();
|
||||||
pairs[i].val = nn_values_nil();
|
pairs[i].val = nn_values_nil();
|
||||||
}
|
}
|
||||||
@ -82,7 +82,7 @@ nn_value nn_values_table(nn_Alloc *alloc, size_t pairCount) {
|
|||||||
return (nn_value) {.tag = NN_VALUE_TABLE, .table = table};
|
return (nn_value) {.tag = NN_VALUE_TABLE, .table = table};
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t nn_values_getType(nn_value val) {
|
nn_size_t nn_values_getType(nn_value val) {
|
||||||
return val.tag;
|
return val.tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,7 +108,7 @@ void nn_values_drop(nn_value val) {
|
|||||||
} else if(val.tag == NN_VALUE_ARRAY) {
|
} else if(val.tag == NN_VALUE_ARRAY) {
|
||||||
val.array->refc--;
|
val.array->refc--;
|
||||||
if(val.array->refc == 0) {
|
if(val.array->refc == 0) {
|
||||||
for(size_t i = 0; i < val.array->len; i++) {
|
for(nn_size_t i = 0; i < val.array->len; i++) {
|
||||||
nn_values_drop(val.array->values[i]);
|
nn_values_drop(val.array->values[i]);
|
||||||
}
|
}
|
||||||
nn_Alloc *a = &val.array->alloc;
|
nn_Alloc *a = &val.array->alloc;
|
||||||
@ -118,7 +118,7 @@ void nn_values_drop(nn_value val) {
|
|||||||
} else if(val.tag == NN_VALUE_TABLE) {
|
} else if(val.tag == NN_VALUE_TABLE) {
|
||||||
val.table->refc--;
|
val.table->refc--;
|
||||||
if(val.table->refc == 0) {
|
if(val.table->refc == 0) {
|
||||||
for(size_t i = 0; i < val.table->len; i++) {
|
for(nn_size_t i = 0; i < val.table->len; i++) {
|
||||||
nn_values_drop(val.table->pairs[i].key);
|
nn_values_drop(val.table->pairs[i].key);
|
||||||
nn_values_drop(val.table->pairs[i].val);
|
nn_values_drop(val.table->pairs[i].val);
|
||||||
}
|
}
|
||||||
@ -129,20 +129,20 @@ void nn_values_drop(nn_value val) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void nn_values_set(nn_value arr, size_t idx, nn_value val) {
|
void nn_values_set(nn_value arr, nn_size_t idx, nn_value val) {
|
||||||
if(arr.tag != NN_VALUE_ARRAY) return;
|
if(arr.tag != NN_VALUE_ARRAY) return;
|
||||||
if(idx >= arr.array->len) return;
|
if(idx >= arr.array->len) return;
|
||||||
nn_values_drop(arr.array->values[idx]);
|
nn_values_drop(arr.array->values[idx]);
|
||||||
arr.array->values[idx] = val;
|
arr.array->values[idx] = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
nn_value nn_values_get(nn_value arr, size_t idx) {
|
nn_value nn_values_get(nn_value arr, nn_size_t idx) {
|
||||||
if(arr.tag != NN_VALUE_ARRAY) return nn_values_nil();
|
if(arr.tag != NN_VALUE_ARRAY) return nn_values_nil();
|
||||||
if(idx >= arr.array->len) return nn_values_nil();
|
if(idx >= arr.array->len) return nn_values_nil();
|
||||||
return arr.array->values[idx];
|
return arr.array->values[idx];
|
||||||
}
|
}
|
||||||
|
|
||||||
void nn_values_setPair(nn_value obj, size_t idx, nn_value key, nn_value val) {
|
void nn_values_setPair(nn_value obj, nn_size_t idx, nn_value key, nn_value val) {
|
||||||
if(obj.tag != NN_VALUE_TABLE) return;
|
if(obj.tag != NN_VALUE_TABLE) return;
|
||||||
if(idx >= obj.table->len) return;
|
if(idx >= obj.table->len) return;
|
||||||
nn_values_drop(obj.table->pairs[idx].key);
|
nn_values_drop(obj.table->pairs[idx].key);
|
||||||
@ -151,14 +151,14 @@ void nn_values_setPair(nn_value obj, size_t idx, nn_value key, nn_value val) {
|
|||||||
obj.table->pairs[idx].val = val;
|
obj.table->pairs[idx].val = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
nn_pair nn_values_getPair(nn_value obj, size_t idx) {
|
nn_pair nn_values_getPair(nn_value obj, nn_size_t idx) {
|
||||||
nn_pair badPair = {.key = nn_values_nil(), .val = nn_values_nil()};
|
nn_pair badPair = {.key = nn_values_nil(), .val = nn_values_nil()};
|
||||||
if(obj.tag != NN_VALUE_TABLE) return badPair;
|
if(obj.tag != NN_VALUE_TABLE) return badPair;
|
||||||
if(idx >= obj.table->len) return badPair;
|
if(idx >= obj.table->len) return badPair;
|
||||||
return obj.table->pairs[idx];
|
return obj.table->pairs[idx];
|
||||||
}
|
}
|
||||||
|
|
||||||
intptr_t nn_toInt(nn_value val) {
|
nn_intptr_t nn_toInt(nn_value val) {
|
||||||
if(val.tag == NN_VALUE_INT) return val.integer;
|
if(val.tag == NN_VALUE_INT) return val.integer;
|
||||||
if(val.tag == NN_VALUE_NUMBER) return val.number;
|
if(val.tag == NN_VALUE_NUMBER) return val.number;
|
||||||
return 0;
|
return 0;
|
||||||
@ -182,8 +182,8 @@ const char *nn_toCString(nn_value val) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *nn_toString(nn_value val, size_t *len) {
|
const char *nn_toString(nn_value val, nn_size_t *len) {
|
||||||
size_t l = 0;
|
nn_size_t l = 0;
|
||||||
const char *c = NULL;
|
const char *c = NULL;
|
||||||
|
|
||||||
if(val.tag == NN_VALUE_CSTR) {
|
if(val.tag == NN_VALUE_CSTR) {
|
||||||
@ -199,26 +199,28 @@ const char *nn_toString(nn_value val, size_t *len) {
|
|||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t nn_measurePacketSize(nn_value *vals, size_t len) {
|
nn_size_t nn_measurePacketSize(nn_value *vals, nn_size_t len) {
|
||||||
size_t size = 0;
|
nn_size_t size = 0;
|
||||||
for(size_t i = 0; i < len; i++) {
|
for(nn_size_t i = 0; i < len; i++) {
|
||||||
nn_value val = vals[i];
|
nn_value val = vals[i];
|
||||||
size += 2;
|
size += 2;
|
||||||
if(val.tag == NN_VALUE_INT || val.tag == NN_VALUE_NUMBER) {
|
if(val.tag == NN_VALUE_INT || val.tag == NN_VALUE_NUMBER) {
|
||||||
size += 8;
|
size += 8;
|
||||||
} else if(val.tag == NN_VALUE_STR) {
|
} else if(val.tag == NN_VALUE_STR) {
|
||||||
size_t len = val.string->len;
|
nn_size_t len = val.string->len;
|
||||||
if(len == 0) len = 1; // ask OC
|
if(len == 0) len = 1; // ask OC
|
||||||
size += len;
|
size += len;
|
||||||
} else if(val.tag == NN_VALUE_CSTR) {
|
} else if(val.tag == NN_VALUE_CSTR) {
|
||||||
size_t len = nn_strlen(val.cstring);
|
nn_size_t len = nn_strlen(val.cstring);
|
||||||
if(len == 0) len = 1; // ask OC
|
if(len == 0) len = 1; // ask OC
|
||||||
size += len;
|
size += len;
|
||||||
} else if(val.tag == NN_VALUE_BOOL || val.tag == NN_VALUE_NIL) {
|
} else if(val.tag == NN_VALUE_BOOL || val.tag == NN_VALUE_NIL) {
|
||||||
size += 4;
|
size += 4;
|
||||||
} else {
|
} else {
|
||||||
// yeah no fuck off
|
// yeah no fuck off
|
||||||
return SIZE_MAX;
|
// we abuse 2's complement
|
||||||
|
// TODO: NN_SIZE_MAX
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return size;
|
return size;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user