bugfix in buffer object. Didn't increase the refcount when slicing

This commit is contained in:
Francesco Cozzuto
2023-01-12 16:11:53 +01:00
parent a56a302fd0
commit 189bfa030b
14 changed files with 117 additions and 315 deletions
+88 -1
View File
@@ -1 +1,88 @@
name = "Francesco";
fun abs(n: Numeric) {
if n < 0:
return -n;
return n;
}
fun min(x, y) {
if x < y:
return x;
return y;
}
fun max(x, y) {
if x > y:
return x;
return y;
}
Func = type(abs);
NFunc = type(print);
Numeric = int | float;
Callable = Func | NFunc;
Collection = List | Map;
# Stringify a number between 0 and 9.
fun stringFromDigit(digit: int) {
assert(digit >= 0 and digit <= 9);
return string.chr(string.ord("0") + digit);
}
# Stringify an unsigned integer
fun stringFromInteger(n: int) {
# This function returns the magnitude of the
# argument and the power of 10 with the same
# magnitude.
fun getIntegerMagnitude(n: int) {
assert(n >= 0);
magn = 0;
powr = 1;
# Basically grow the power until it's
# bigger than the input.
while n >= powr * 10: {
powr = 10 * powr;
magn = 1 + magn;
}
return magn, powr;
}
negative = (n < 0);
# Get the power of 10 with the
# magnitude of the input.
_, power = getIntegerMagnitude(n);
temp = abs(n); # Temporary copy of the input
text = ""; # The output we're about to compute
while power >= 1: {
# Pop the leftmost digit from [temp].
digit = temp / power; # Get the leftmost digit
temp = temp % power; # Remove it
# Add the digit to the output.
text = string.cat(text, stringFromDigit(digit));
# Prep for the next iteration.
power = power / 10;
}
if negative:
text = string.cat("-", text);
return text;
}
fun floatFromInteger(n: int)
return 1.0 * n;
fun integerFromDigit(char: String) {
res = ord(char) - ord('0');
if res < 0 or res > 9:
error("String isn't a digit");
return res;
}
fun append(array: List, item)
array[count(array)] = item;
+16 -1
View File
@@ -82,7 +82,7 @@ static _Bool interpret(Executable *exe)
CompilationErrorType errtyp;
Executable *prelude_exe = compile(prelude, (Error*) &error, &errtyp);
if(exe == NULL) {
if(prelude_exe == NULL) {
const char *errname;
switch(errtyp) {
default:
@@ -109,6 +109,21 @@ static _Bool interpret(Executable *exe)
}
Object *noja_bins = rets[0];
// Need to remake the native built-ins because
// running the script invalidated the previous
// pointer.
native_bins = Object_NewStaticMap(bins_basic, bins_basic_init, runt, (Error*) &error);
if(native_bins == NULL)
{
assert(error.base.occurred == 1);
print_error(NULL, (Error*) &error);
RuntimeError_Free(&error);
Runtime_Free(runt);
Source_Free(prelude);
Executable_Free(prelude_exe);
return 0;
}
Object *all_bins = Object_NewClosure(native_bins, noja_bins, Runtime_GetHeap(runt), (Error*) &error);
if (all_bins == NULL) {
print_error(NULL, (Error*) &error);
+1
View File
@@ -170,6 +170,7 @@ void Heap_Free(Heap *heap)
// it now though.
Error_Free(&error);
Error_Init(&error);
// Continue the loop..
}
}
+2 -1
View File
@@ -108,7 +108,7 @@ static _Bool buffer_free(Object *self, Error *error)
UNUSED(error);
BufferObject *buffer = (BufferObject*) self;
Payload *payload = buffer->payload;
ASSERT(payload != NULL && payload->refs > 0);
@@ -142,6 +142,7 @@ Object *Object_SliceBuffer(Object *obj, size_t offset, size_t length, Heap *heap
if(slice == NULL)
return NULL;
payload->refs++;
slice->payload = payload;
slice->offset = offset;
slice->length = length;
+2 -2
View File
@@ -57,9 +57,9 @@ _Bool Object_IsFile(Object *obj)
FILE *Object_GetStream(Object *obj)
{
if(!Object_IsDir(obj)) {
if(!Object_IsFile(obj)) {
Error_Panic("%s expected a " TYPENAME_FILE
"object, but an %s was provided",
" object, but an %s was provided",
__func__, Object_GetName(obj));
return NULL;
}