Commit Graph

20 Commits

Author SHA1 Message Date
Bob Nystrom
4d0b3f78a5 feat: add wrenGetVariable API to load top-level module variable into a slot
Implement wrenGetVariable function in wren_vm.c that looks up a top-level variable by name in a specified module and stores it in the given slot. Includes validation for non-null module/name parameters and slot bounds, module resolution via getModule, variable lookup through symbol table, and slot assignment. Adds corresponding declaration to wren.h public header. Introduces comprehensive test suite in test/api/get_variable.* covering scenarios: retrieving variable before definition (null), after definition, after reassignment, using a non-zero slot, and accessing a variable from another module. Registers the test bindings in test/api/main.c and updates Xcode project file to include the new source.
2015-12-26 18:53:14 +00:00
Bob Nystrom
51bc3a828c feat: add wrenGetValueDouble API and benchmark for wrenCall() performance
Add a new public API function wrenGetValueDouble() to extract numeric values from WrenValue handles, with proper assertions for NULL and type checking. Introduce a comprehensive benchmark test for wrenCall() that measures invocation overhead by calling a foreign method 1,000,000 times across a separate VM instance, validating result correctness and reporting elapsed time. Include the benchmark script api_call.wren and register it in the Python benchmarking harness. Also fix minor assertion message typos in wren_vm.c for consistency.
2015-12-24 01:29:53 +00:00
Bob Nystrom
001a9b7439 docs: add detailed slot-based API documentation for foreign method interface in wren.h 2015-12-17 00:39:27 +00:00
Bob Nystrom
23b6980dfd feat: add wrenEnsureSlots, wrenSetSlotNewList, and wrenInsertInList C API functions for list manipulation
Add three new public C API functions to the Wren embedding API that enable
foreign methods to create and populate lists. wrenEnsureSlots grows the
foreign slot stack to guarantee space for temporary work, wrenSetSlotNewList
stores a new empty list in a slot, and wrenInsertInList inserts a value from
one slot into a list at another slot at a given index (supporting negative
indices for relative positioning). The implementation includes a new internal
ensureStack helper that reallocates the fiber's stack and updates all
pointers (stack top, call frame starts, open upvalues, and foreign stack
start) when the reallocation moves the stack. New test files (lists.c,
lists.h, lists.wren) exercise creation, appending, insertion, and negative
index insertion, and the existing slots test is extended with an ensure test
that verifies slot count growth and slot usability.
2015-12-17 00:28:26 +00:00
Bob Nystrom
7535fed608 refactor: replace wrenReturn___() with wrenSetSlot___() for writing values to arbitrary slots
The old wrenReturn___() functions only wrote to slot 0 for return values. The new wrenSetSlot___() functions allow writing to any slot index, making them general-purpose for manipulating the foreign call stack. This change updates all call sites, removes the dedicated returns test suite in favor of slots tests, and adjusts the VM internals to treat slot 0 as the return position rather than using a separate return mechanism.
2015-12-16 21:00:13 +00:00
Bob Nystrom
234a98faef refactor: rename wrenGetArgument* to wrenGetSlot* and add IS_LIST macro for slot-based API
Migrate the foreign method argument access API from an argument-oriented naming
scheme to a slot-based one, renaming wrenGetArgumentCount to wrenGetSlotCount,
wrenGetArgumentBool to wrenGetSlotBool, and all other argument accessors
accordingly. Update every call site across the VM, modules (io, timer, meta,
random), and test files (benchmark, foreign_class) to use the new names. The
slot getters now assert the correct type at runtime rather than silently
returning defaults, shifting safety responsibility to the caller for
performance. Also add the IS_LIST type-checking macro to wren_value.h alongside
the existing IS_* macros.
2015-12-16 18:22:42 +00:00
Bob Nystrom
8b70246acd refactor: remove sourcePath parameter from wrenInterpret and related module functions
The sourcePath field on ObjModule was redundant since it always matched the module name except for the main script. This change removes the parameter from wrenInterpret, wrenInterpretInModule, and wrenNewModule, and replaces all references to module->sourcePath with module->name in error reporting and debug output.
2015-10-16 01:17:42 +00:00
Bob Nystrom
f6c36ec85b feat: add va_list version of wrenCall and fix GC bug with return values
- Introduce wrenCallVarArgs as an explicit va_list variant, allowing variadic functions to forward their arguments directly without re-parsing.
- Fix a garbage collection issue in wrenCall where return values could be prematurely collected by ensuring proper root handling.
- Refactor the call API test to avoid re-entering the VM by moving test execution into an afterLoad callback instead of a foreign method.
2015-09-30 02:29:10 +00:00
Bob Nystrom
b19d85f618 feat: add 'a' format specifier and null-value support to wrenCall, plus tests for strings with null bytes
Add a new 'a' format specifier to wrenCall that accepts an explicit byte array and length, enabling callers to pass strings containing null bytes without truncation. Also allow passing NULL for 'v' specifier arguments to produce a Wren NULL value. Rename setForeignCallbacks to setTestCallbacks for clarity. Extend the test suite with a new call.c test file that exercises all argument types including 'a' and 'v' with NULL, and add returnString/returnBytes foreign methods to verify wrenReturnString handles both null-terminated and explicit-length strings correctly.
2015-09-24 15:02:31 +00:00
Bob Nystrom
ffe87c271d feat: add configurable write callback for System.print output delegation
Add a `WrenWriteFn` callback to `WrenConfiguration` that allows host applications to control how text from `System.print()` and related functions is displayed. When set, the VM invokes this callback instead of directly calling `printf`. If the callback is NULL, printed text is silently discarded. The CLI's `initVM()` now registers a simple `write` function that prints to stdout, while the core `system_writeString` primitive checks for the callback before outputting. This change also refactors `wrenInitConfiguration` to set `defaultReallocate` as the default reallocation function and consolidates configuration fields into a single `WrenConfiguration` struct stored directly in `WrenVM`, removing separate member fields for reallocate, bindForeignMethod, bindForeignClass, loadModule, minNextGC, and heapScalePercent.
2015-09-16 06:01:43 +00:00
Bob Nystrom
4665ec1913 feat: implement foreign object finalization and expose wrenCollectGarbage API
Add finalizer invocation for foreign objects during garbage collection by calling wrenFinalizeForeign in wrenFreeObj. Expose wrenCollectGarbage as a public API function and update internal calls accordingly. Ensure the "<finalize>" symbol is always registered in the symbol table even when no finalizer is provided. Add test fixtures for Resource class with finalizer and Api.gc/Api.finalized methods to verify finalization behavior across multiple GC cycles.
2015-09-01 04:56:21 +00:00
Bob Nystrom
2a8847fa57 feat: replace WrenMethod with WrenValue for method handles and update wrenCall signature to return WrenInterpretResult 2015-08-31 14:57:48 +00:00
Bob Nystrom
2dc209e585 feat: add foreign class support with allocation, construct opcodes, and CLI callback wiring
Implement the initial infrastructure for foreign classes in the Wren VM, including new opcodes (FOREIGN_CONSTRUCT, FOREIGN_CLASS), a WrenBindForeignClassFn callback type in the public API, and updated CLI vm.c to store and forward foreign binding callbacks via setForeignCallbacks(). The compiler now tracks whether a class is foreign via a new isForeign flag in ClassCompiler, emits CODE_FOREIGN_CONSTRUCT instead of CODE_CONSTRUCT for foreign class constructors, and errors on field definitions inside foreign classes. The debug dump and opcode header gain entries for the new opcodes, and wrenBindSuperclass handles the -1 numFields sentinel for foreign classes to prevent field inheritance from superclasses.
2015-08-15 19:07:53 +00:00
Bob Nystrom
f9f8b55b29 feat: add support for passing WrenValue pointers as 'v' type argument to wrenCall
Extend the wrenCall variadic argument handling to accept a new 'v' type specifier,
which allows callers to pass a previously acquired WrenValue* directly without
implicitly releasing it. This enables reusing existing Wren values across multiple
calls without unnecessary string or number conversions.

The implementation reads a WrenValue* from the variadic arguments and extracts
its internal Value for the call. Additionally, fix the parameter counting logic
in makeCallStub to correctly count underscore parameters only within the
parenthesized signature portion, preventing off-by-one errors for methods with
trailing parentheses in their signature strings.
2015-08-07 14:57:23 +00:00
Bob Nystrom
592772a97d feat: add WrenValue handle API for persistent object references outside VM
Introduce WrenValue type and wrenGetArgumentValue/wrenReleaseValue functions to allow C code to hold GC-safe references to Wren objects across foreign calls. Implement doubly-linked list management in VM struct, mark value handles during garbage collection, and add assertion in wrenFreeVM to detect unreleased values. Update test infrastructure with new get_value test case and rename existing test binding functions to camelCase convention.
2015-08-07 05:24:15 +00:00
Patricio Mac Adden
349a3307aa fix: correct typo in wren.h comment from 'orde' to 'order' in wrenGetMethod docs 2015-04-18 02:13:36 +00:00
Bob Nystrom
2b3381e0b2 feat: add Meta class with eval method and integrate into VM and build system 2015-03-26 03:06:55 +00:00
Bob Nystrom
90de831bd7 refactor: remove oldSize parameter from WrenReallocateFn and all call sites
Simplify the allocation API by dropping the unused `oldSize` argument from `WrenReallocateFn` typedef, its documentation, and all internal callers. Update `wrenReallocate` in `wren_vm.c` to pass only `newSize`, remove the `defaultReallocate` wrapper in favor of direct `realloc`, and adjust `generate_baseline` signature in benchmark script to match.
2015-03-25 14:45:29 +00:00
Bob Nystrom
a350d7c26a feat: add foreign method binding infrastructure with IO library integration
Implement the `WrenBindForeignMethodFn` callback type and `bindForeignMethodFn` configuration field to allow host applications to resolve foreign method declarations at class definition time. Refactor the IO library to use this new binding mechanism instead of the previous `wrenDefineStaticMethod` approach, adding `foreign` keyword support to the lexer and compiler. Introduce `freeCompiler` helper for cleanup, store module names in `ObjModule`, and update the VM to call the binder when processing `CODE_METHOD_INSTANCE` and `CODE_METHOD_STATIC` opcodes for foreign methods.
2015-03-24 14:58:15 +00:00
Bob Nystrom
18cf57d5f5 refactor: split source tree into vm/ and cli/ directories with updated build system
Separate Wren VM library sources from CLI tool sources by moving them into
src/vm/ and src/cli/ subdirectories respectively. Update the Makefile to use
separate wildcard patterns for each directory, generate distinct object file
paths under build/vm/ and build/cli/, and adjust include paths to src/include.
Also update the Xcode project file to reference the new file locations.
2015-03-14 22:00:50 +00:00