Commit Graph

17 Commits

Author SHA1 Message Date
Bob Nystrom
07c3dff92f fix: track live memory via bytesAllocated instead of subtracting freed sizes
The old code subtracted oldSize from totalAllocated during deallocation,
which required knowing the object size at free time. This was unsafe
because it could read a freed object to determine its size. The new
approach tracks bytesAllocated by only adding new allocations and
relying on GC marking to account for freed memory, making it both
correct and faster with less code.
2013-12-14 22:17:16 +00:00
Bob Nystrom
fc3ebb924c refactor: replace raw #ifdef NAN_TAGGING with WREN_NAN_TAGGING flag and add WREN_COMPUTED_GOTO, WREN_DEBUG_GC_STRESS, WREN_TRACE_MEMORY configurable defines
Convert all conditional compilation guards from bare `#ifdef NAN_TAGGING` to `#if WREN_NAN_TAGGING` across wren_common.h, wren_value.c, wren_value.h, and wren_vm.c. Introduce new `#ifndef`-based defaults for WREN_NAN_TAGGING, WREN_COMPUTED_GOTO, WREN_DEBUG_GC_STRESS, and WREN_TRACE_MEMORY in wren_common.h, replacing the old TODO comments and commented-out debug flags. Update preprocessor indentation style for consistency.
2013-12-14 19:06:54 +00:00
Bob Nystrom
d7a0063f4b feat: replace constructor method special-casing with dedicated CODE_NEW instruction
Remove the METHOD_CTOR method type and CODE_METHOD_CTOR opcode, replacing them with a new CODE_NEW instruction that creates class instances directly in the bytecode. This simplifies the compiler by emitting CODE_NEW at the start of constructor bodies instead of relying on a separate method dispatch path, and eliminates the redundant METHOD_CTOR enum value from the method lookup logic.
2013-12-13 19:32:47 +00:00
Bob Nystrom
42b32acef4 feat: implement superclass method dispatch with new SUPER bytecodes and named call compiler
Add CODE_SUPER_0 through CODE_SUPER_16 bytecodes to the VM for invoking inherited methods, along with a `namedCall` compiler helper that parses method names and argument lists after a dot. The interpreter dispatches super calls by walking up the class hierarchy from the receiver's immediate class, skipping methods defined on that class. Includes three test cases covering calling a different superclass method, calling the same method name, and indirectly inherited methods. Also fixes error formatting to handle newline tokens gracefully and adds a TODO note about not deduplicating placeholder constants for super calls.
2013-12-13 16:37:49 +00:00
Bob Nystrom
a7dbdc4cba perf: reorder opcode switch cases to move less frequent CODE_LIST and CODE_CLOSURE lower
Reorder the opcode enum values, dispatch table entries, switch-case handlers, and debug dump cases for CODE_LIST and CODE_CLOSURE to appear after CODE_RETURN and before CODE_CLASS, improving interpreter performance by reducing branch prediction misses for the more common instructions that now appear earlier in the switch.
2013-12-13 14:47:34 +00:00
Bob Nystrom
5e62b5856e perf: reorder MethodType enum and switch cases to improve branch prediction
Reorder the `MethodType` enum values so that `METHOD_NONE` is placed last instead of first,
and correspondingly move the `METHOD_NONE` case in the interpreter's dispatch switch to the
end of the case list. This places the most frequently executed method types (`METHOD_PRIMITIVE`,
`METHOD_FOREIGN`, `METHOD_BLOCK`, `METHOD_CTOR`) earlier in both the enum and the switch,
which can improve branch prediction and instruction cache locality in the hot dispatch loop.
2013-12-12 15:39:27 +00:00
Bob Nystrom
c107d26c93 feat: generate default constructors at compile time and reorder interpret loop for branch alignment
Add a `defaultConstructor` function in the compiler that emits bytecode for a constructor returning the receiver, invoked during class compilation. Reorder `CODE_CLASS`, `CODE_SUBCLASS`, and method opcodes to the end of the interpret loop's jump table and switch cases, moving `CODE_CALL_0` through `CODE_CALL_16` earlier to compensate for branch alignment issues caused by removing code from `CODE_CLASS` and `CODE_CALL` handlers.
2013-12-12 15:34:16 +00:00
Bob Nystrom
eb97920905 docs: expand TODO comment on default constructor performance tradeoff in wren_vm.c 2013-12-11 16:00:25 +00:00
Bob Nystrom
d8c31a4391 feat: add wrenBindMethod to patch field indices and superclass dispatch in inherited methods
When a class is defined, its superclass is not known until runtime since class definitions are imperative statements. This adds a new wrenBindMethod function that walks the bytecode of a method after it is bound to a class, adjusting LOAD_FIELD/STORE_FIELD indices to account for inherited fields and patching superclass dispatch targets. The newClass function now correctly accumulates numFields from the superclass chain, and the VM calls wrenBindMethod when binding methods to a class. Includes new test files for inherited field access and the is operator.
2013-12-11 00:13:25 +00:00
Bob Nystrom
e910186f51 fix: remove stack pop for loop body in LOOP opcode since bodies are now statements 2013-12-08 02:47:40 +00:00
Bob Nystrom
5b05c477c6 feat: implement closures with upvalue support and statement/expression separation
Add closure objects that wrap functions with captured upvalues, enabling
first-class closures that close over local variables from enclosing scopes.
Introduce new bytecode instructions (CODE_CLOSURE, CODE_LOAD_UPVALUE,
CODE_STORE_UPVALUE, CODE_CLOSE_UPVALUE, CODE_RETURN) and a CompilerUpvalue
tracking system with a MAX_UPVALUES limit of 256. Refactor the grammar to
strictly separate statements from expressions, fixing a bug where block
expressions incorrectly popped locals while temporaries remained on the
stack. Rename internal functions (wrenCallFunction, wrenDebugDumpInstruction,
wrenDebugDumpStack) and add Upvalue allocation, closure creation, and
debug dump support for the new instructions.
2013-12-04 15:43:50 +00:00
Bob Nystrom
ed005573ad refactor: unify compiler end-of-chunk code path into single endCompiler function
Extract the common bytecode finalization logic into a dedicated endCompiler function that emits CODE_END and handles parent compiler function loading, replacing duplicated inline code. Update VM method definition opcodes to pop function from stack instead of reading from constants, enabling future closure support. Add TODO comments outlining planned closure capture mechanism and upvalue tracking requirements.
2013-12-01 22:59:56 +00:00
Bob Nystrom
eaf69b892f feat: replace direct malloc/free calls with user-provided allocator in VM
The `wrenNewVM` function now uses the provided `reallocateFn` for fiber allocation instead of calling `malloc` directly, and stores the allocator in `vm->reallocate` for later use. The `wrenReallocate` function delegates all memory operations (including freeing) to the stored allocator, removing the fallback to `realloc` and `free`. This ensures consistent memory management through the external allocator, enabling custom allocation strategies like memory pools or tracking.
2013-12-01 18:22:32 +00:00
Bob Nystrom
88f7444f0c feat: extract bytecode dumpInstruction into new wren_debug module
Move the dumpInstruction function and its associated opcode printing logic from wren_vm.c into a dedicated wren_debug.c source file with a corresponding wren_debug.h header. Add the new source file to the Xcode project build configuration.
2013-11-30 00:17:33 +00:00
Bob Nystrom
230017a616 feat: raise max method parameters from 10 to 16 with proper error handling
Add MAX_PARAMETERS constant set to 16 and extend VM bytecode instructions CODE_CALL_11 through CODE_CALL_16 to support the new limit. Introduce compile-time error messages when parameter or argument count exceeds 16 in function definitions, method declarations, subscript calls, and regular method calls. Add new native fiber call stubs fn_call9 through fn_call16 and register them in the core initialization. Include comprehensive test coverage for 9-16 parameter functions and methods, plus error tests for exceeding the limit in all contexts.
2013-11-29 23:08:27 +00:00
Bob Nystrom
fe7e9041d1 refactor: replace hardcoded pinned array with linked list in WrenVM GC root tracking
Replace the fixed-size `MAX_PINNED` array of `Obj*` pointers with a dynamically allocated linked list of `PinnedObj` nodes, removing the 16-object limit and eliminating the `numPinned` counter. Update `pinObj` to accept a caller-provided `PinnedObj` stack variable and link it at the head of the list, and simplify `unpinObj` to pop the head without requiring the object argument. Adjust `wrenNewVM` to initialize the new `pinned` pointer to NULL, and modify `collectGarbage` to iterate the linked list instead of the array.
2013-11-29 17:14:19 +00:00
Bob Nystrom
f9c534aef9 chore: rename source files to wren_ prefix and add module-level documentation headers 2013-11-28 16:11:50 +00:00