mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
I've ripped out the deep copying, and flattened the bucket usage, but
this results in no memory being freed or reused for the lifetime of the
program.
This is shown most clearly with this script:
```toy
fn makeCounter() {
var counter: int = 0;
fn increment() {
return ++counter;
}
return increment;
}
var tally = makeCounter();
while (true) {
var result = tally();
if (result >= 10_000_000) {
break;
}
}
```
The number of calls vs amount of memory consumed is:
```
function calls -> memory used in megabytes
1_000 -> 0.138128
10_000 -> 2.235536
100_000 -> 21.7021
1_000_000 -> 216.1712
10_000_000 -> 1520.823
```
Obviously this needs to be fixed, as ballooning to gigabytes of memory
in only a moment isn't practical. That will be the next task - to find
some way to free memory that isn't needed anymore.
See #163, #160
58 lines
1.3 KiB
C
58 lines
1.3 KiB
C
#pragma once
|
|
|
|
#include "toy_common.h"
|
|
|
|
#include "toy_bucket.h"
|
|
#include "toy_scope.h"
|
|
#include "toy_module.h"
|
|
|
|
#include "toy_value.h"
|
|
#include "toy_string.h"
|
|
#include "toy_stack.h"
|
|
#include "toy_array.h"
|
|
#include "toy_table.h"
|
|
#include "toy_function.h"
|
|
|
|
typedef struct Toy_VM {
|
|
//raw instructions to be executed
|
|
unsigned char* code;
|
|
|
|
//metadata
|
|
unsigned int jumpsCount;
|
|
unsigned int paramCount;
|
|
unsigned int dataCount;
|
|
unsigned int subsCount;
|
|
|
|
unsigned int codeAddr;
|
|
unsigned int jumpsAddr;
|
|
unsigned int paramAddr;
|
|
unsigned int dataAddr;
|
|
unsigned int subsAddr;
|
|
|
|
//execution utils
|
|
unsigned int programCounter;
|
|
|
|
//scope - block-level key/value pairs
|
|
Toy_Scope* scope;
|
|
|
|
//stack - immediate-level values only
|
|
Toy_Stack* stack;
|
|
|
|
//easy access to memory
|
|
Toy_Bucket* memoryBucket;
|
|
Toy_Bucket** parentBucketHandle;
|
|
} Toy_VM;
|
|
|
|
TOY_API void Toy_resetVM(Toy_VM* vm, bool preserveScope);
|
|
|
|
TOY_API void Toy_initVM(Toy_VM* vm); //creates memory
|
|
TOY_API void Toy_inheritVM(Toy_VM* vm, Toy_VM* parent); //inherits scope bucket
|
|
|
|
TOY_API void Toy_bindVM(Toy_VM* vm, Toy_Module* module, bool preserveScope);
|
|
TOY_API unsigned int Toy_runVM(Toy_VM* vm);
|
|
TOY_API void Toy_freeVM(Toy_VM* vm);
|
|
|
|
TOY_API Toy_Array* Toy_extractResultsFromVM(Toy_VM* subVM, unsigned int resultCount);
|
|
|
|
//TODO: inject extra data (hook system for external libraries)
|