Skip to content

Commit

Permalink
Documentation and minor tweak to allocation size limit
Browse files Browse the repository at this point in the history
  • Loading branch information
coder-mike committed Jul 2, 2022
1 parent 9dd9718 commit 3f8a47a
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 13 deletions.
6 changes: 3 additions & 3 deletions dist-c/microvium.c
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ typedef MVM_LONG_PTR_TYPE LongPtr;
// Offset of field in a struct
#define OFFSETOF(TYPE, ELEMENT) ((uint16_t)(uintptr_t)&(((TYPE *)0)->ELEMENT))

// Allocation
// Maximum size of an allocation (4kB)
#define MAX_ALLOCATION_SIZE 0xFFF

// This is the only valid way of representing NaN
Expand Down Expand Up @@ -4048,7 +4048,7 @@ static void* gc_allocateWithHeader(VM* vm, uint16_t sizeBytes, TeTypeCode typeCo
uint16_t* p;
uint16_t* end;

if (sizeBytes > 0xFFF - 3) {
if (sizeBytes >= (MAX_ALLOCATION_SIZE + 1)) {
CODE_COVERAGE_ERROR_PATH(353); // Not hit
MVM_FATAL_ERROR(vm, MVM_E_ALLOCATION_TOO_LARGE);
} else {
Expand Down Expand Up @@ -7479,7 +7479,7 @@ static mvm_TeError vm_uint8ArrayNew(VM* vm, Value* slot) {

mvm_Value mvm_uint8ArrayFromBytes(mvm_VM* vm, const uint8_t* data, size_t sizeBytes) {
CODE_COVERAGE(346); // Hit
if (sizeBytes > 0xFFF - 3) {
if (sizeBytes >= (MAX_ALLOCATION_SIZE + 1)) {
MVM_FATAL_ERROR(vm, MVM_E_ALLOCATION_TOO_LARGE);
return VM_VALUE_UNDEFINED;
}
Expand Down
27 changes: 27 additions & 0 deletions doc/supported-builtins.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Supported Builtins

### Standard builtin functions and objects

`Reflect.ownKeys` - returns an array of keys for an object (only supported on non-array, non-function objects)

## Additional builtin function and objects

### vmExport(id, func)

Export a function to be accessible to the host at the given ID.

The ID can be any integer in the range 0 to 65535.

(This function is only available at compile-time)

### vmImport(id)

Import a host function to be accessed by JS code in the VM.

The ID can be any integer in the range 0 to 65535.

(This function is only available at compile-time)

### Microvium.newUint8Array(size)

Create a Uint8Array buffer with the given size. Sizes up to 4095 bytes are supported.
1 change: 1 addition & 0 deletions doc/supported-language.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Note: the most up-to-date authority on supported features is the [set of test sc
- Modules, with `import` and `export` statements
- `throw` (but not `try`/`catch`)
- `Reflect.ownKeys`
- See also [supported builtins](./supported-builtins.md)

## NOT Supported

Expand Down
1 change: 0 additions & 1 deletion lib/virtual-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2111,7 +2111,6 @@ export class VirtualMachine {
which can't be generated syntactically. */
const obj_Microvium = this.newObject()
this.globalSet('Microvium', obj_Microvium)
// WIP: I need documentation for these builtins
this.setProperty(obj_Microvium, this.stringValue('newUint8Array'), this.importCustomILFunction('Microvium.newUint8Array', {
entryBlockID: 'entry',
blocks: {
Expand Down
4 changes: 2 additions & 2 deletions native-vm/microvium.c
Original file line number Diff line number Diff line change
Expand Up @@ -2642,7 +2642,7 @@ static void* gc_allocateWithHeader(VM* vm, uint16_t sizeBytes, TeTypeCode typeCo
uint16_t* p;
uint16_t* end;

if (sizeBytes > 0xFFF - 3) {
if (sizeBytes >= (MAX_ALLOCATION_SIZE + 1)) {
CODE_COVERAGE_ERROR_PATH(353); // Not hit
MVM_FATAL_ERROR(vm, MVM_E_ALLOCATION_TOO_LARGE);
} else {
Expand Down Expand Up @@ -6073,7 +6073,7 @@ static mvm_TeError vm_uint8ArrayNew(VM* vm, Value* slot) {

mvm_Value mvm_uint8ArrayFromBytes(mvm_VM* vm, const uint8_t* data, size_t sizeBytes) {
CODE_COVERAGE(346); // Hit
if (sizeBytes > 0xFFF - 3) {
if (sizeBytes >= (MAX_ALLOCATION_SIZE + 1)) {
MVM_FATAL_ERROR(vm, MVM_E_ALLOCATION_TOO_LARGE);
return VM_VALUE_UNDEFINED;
}
Expand Down
2 changes: 1 addition & 1 deletion native-vm/microvium_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ typedef MVM_LONG_PTR_TYPE LongPtr;
// Offset of field in a struct
#define OFFSETOF(TYPE, ELEMENT) ((uint16_t)(uintptr_t)&(((TYPE *)0)->ELEMENT))

// Allocation
// Maximum size of an allocation (4kB)
#define MAX_ALLOCATION_SIZE 0xFFF

// This is the only valid way of representing NaN
Expand Down
2 changes: 1 addition & 1 deletion size-test/output/size.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
text data bss dec hex filename
9260 0 0 9260 242c output/microvium.o
9256 0 0 9256 2428 output/microvium.o
10 changes: 5 additions & 5 deletions test/getting-started/code/microvium/microvium.c
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ typedef MVM_LONG_PTR_TYPE LongPtr;
// Offset of field in a struct
#define OFFSETOF(TYPE, ELEMENT) ((uint16_t)(uintptr_t)&(((TYPE *)0)->ELEMENT))

// Allocation
// Maximum size of an allocation (4kB)
#define MAX_ALLOCATION_SIZE 0xFFF

// This is the only valid way of representing NaN
Expand Down Expand Up @@ -4048,7 +4048,7 @@ static void* gc_allocateWithHeader(VM* vm, uint16_t sizeBytes, TeTypeCode typeCo
uint16_t* p;
uint16_t* end;

if (sizeBytes > 0xFFF - 3) {
if (sizeBytes >= (MAX_ALLOCATION_SIZE + 1)) {
CODE_COVERAGE_ERROR_PATH(353); // Not hit
MVM_FATAL_ERROR(vm, MVM_E_ALLOCATION_TOO_LARGE);
} else {
Expand Down Expand Up @@ -7478,8 +7478,8 @@ static mvm_TeError vm_uint8ArrayNew(VM* vm, Value* slot) {
}

mvm_Value mvm_uint8ArrayFromBytes(mvm_VM* vm, const uint8_t* data, size_t sizeBytes) {
CODE_COVERAGE_UNTESTED(346); // Not hit
if (sizeBytes > 0xFFF - 3) {
CODE_COVERAGE(346); // Hit
if (sizeBytes >= (MAX_ALLOCATION_SIZE + 1)) {
MVM_FATAL_ERROR(vm, MVM_E_ALLOCATION_TOO_LARGE);
return VM_VALUE_UNDEFINED;
}
Expand All @@ -7491,7 +7491,7 @@ mvm_Value mvm_uint8ArrayFromBytes(mvm_VM* vm, const uint8_t* data, size_t sizeBy
}

mvm_TeError mvm_uint8ArrayToBytes(mvm_VM* vm, mvm_Value uint8ArrayValue, uint8_t** out_data, size_t* out_size) {
CODE_COVERAGE_UNTESTED(348); // Not hit
CODE_COVERAGE(348); // Hit

// Note: while it makes sense to allow Uint8Arrays in general to live in ROM,
// I think we can require that those that hit the FFI boundary are never
Expand Down

0 comments on commit 3f8a47a

Please sign in to comment.