Commit Graph

18 Commits

Author SHA1 Message Date
Bob Nystrom
15e965c5eb fix: add null and already-freed guard to wrenFreeVM to prevent double-free crash
Add early return check in wrenFreeVM that validates the VM pointer is not NULL
and that the methodNames count is non-zero, preventing a crash when the VM
has already been freed or was never properly initialized.
2015-03-20 14:56:32 +00:00
Bob Nystrom
398a693d3c fix: replace constant-time string hash with full O(n) scan over all bytes 2015-03-19 14:38:05 +00:00
Bob Nystrom
b33f16152a feat: move contains method from List to Sequence and add tests for list and range
Add a generic `contains(element)` method to the `Sequence` class that iterates over elements using the existing `for` loop protocol, removing the duplicate implementation from `List`. Include comprehensive test files for both `List.contains` and `Range.contains` covering ordered, backwards, and exclusive ranges. Also fix the `generate_builtins.py` script to write to the correct `src/vm/` directory path and add a `float:left` CSS rule to the main content area in the documentation stylesheet.
2015-03-19 14:29:07 +00:00
Bob Nystrom
550fd13565 chore: relocate AS_* and IS_* macros to top of wren_value.h for better organization 2015-03-19 14:28:53 +00:00
Bob Nystrom
a15bf3136e feat: move List.contains to Sequence.contains and add tests for List and Range
The contains method is promoted from List to the Sequence base class, making it available to all sequence types (including Range). Tests are added for both List.contains and Range.contains, covering ordered, backwards, and exclusive ranges.
2015-03-18 14:54:55 +00:00
Bob Nystrom
1e842bbb2e feat: store precomputed hash in ObjString and use it for equality and hashing
Cache the FNV-1a hash code in the ObjString struct at creation time, replacing the
on-the-fly sampling hash in hashObject() and enabling constant-time string equality
checks. Refactor string allocation into allocateString() and hashString() helpers,
remove the old wrenNewUninitializedString() declaration, and add a CONST_STRING
macro. Disable the unimplemented subscript range operator with a runtime error and
skip the related UTF-8 tests. Add a string_equals benchmark to measure the speedup.
2015-03-18 14:09:03 +00:00
Bob Nystrom
a3418dc06a feat: swap list insert arguments to index-first order and update all call sites
Swap the parameter order of `List.insert()` from `(item, index)` to `(index, item)`,
matching the convention used by most standard libraries. Update the C implementation
in `wren_core.c`, all documentation examples, and every test file to reflect the new
signature. Also restructure MSVC project files to match the new `src/cli/` and
`src/vm/` directory layout, add Visual Studio cache files to `.gitignore`, and fix
a trailing whitespace issue in `class_supertype`.
2015-03-17 14:01:09 +00:00
Bob Nystrom
f6da4700f4 fix: guard string hash loop against zero-length strings causing division by zero 2015-03-17 14:00:54 +00:00
Bob Nystrom
0d42d87689 feat: reverse List.insert argument order to index-first, value-second
The `List.insert` method signature is changed from `insert(item, index)` to `insert(index, item)`, aligning with standard practice in most languages. All documentation, core implementation, and test files are updated to reflect the new parameter order. The `validateIndex` call now reads the index from slot 1 instead of slot 2, and the value is read from slot 2.
2015-03-16 16:18:59 +00:00
Bob Nystrom
db365ae9fa fix: correct object type label in dumpObject for OBJ_RANGE case
The dumpObject function in wren_debug.c was incorrectly printing "[fn %p]" for
OBJ_RANGE objects instead of the correct "[range %p]" label. This fix ensures
that range objects are properly identified during debug output.
2015-03-16 14:05:40 +00:00
Bob Nystrom
a73ee9a8e7 fix: pass missing argName to wrenStringFormat in validateIntValue and validateIndexValue
The two validation functions in wren_core.c were calling wrenStringFormat with a format
string containing a "$" placeholder but were not passing the argName argument, causing
the placeholder to remain unsubstituted in error messages. This commit adds the missing
argName parameter to both calls.

Additionally, the CONST_STRING macro in wren_value.h was changed from using strlen(text)
to sizeof(text) - 1, which allows the compiler to compute the string length at compile
time rather than at runtime, improving performance for string literal creation.
2015-03-16 06:17:08 +00:00
Bob Nystrom
e95b32188f refactor: replace sizeof-based string length with strlen in CONST_STRING macro
The previous implementation used `sizeof(text) - 1` to determine string length at compile time, which required the argument to be a bare string literal. The new approach uses `strlen(text)` instead, which allows most compilers to evaluate the string length at compile time when a string literal is passed, while also supporting non-literal string arguments. This change trades compile-time guarantee for broader compatibility and compiler optimization potential.
2015-03-16 06:07:20 +00:00
Bob Nystrom
9314cbfd72 refactor: replace manual string concatenation with wrenStringFormat across VM core
Replace ad-hoc string construction using wrenNewString, wrenStringConcat, and sprintf with the new wrenStringFormat helper. This centralizes string formatting logic, reduces code duplication, and eliminates manual length calculations in wren_compiler.c, wren_core.c, wren_value.c, and wren_vm.c. Also introduces CONST_STRING macro for compile-time string literals and changes wrenNewUninitializedString return type from Value to ObjString* for consistency.
2015-03-16 05:32:20 +00:00
Thorbjørn Lindeijer
9a01d4b489 feat: reverse argument order of List.insert to index-first convention
The previous signature `insert(element, index)` was counter-intuitive and inconsistent with every major language's list API. This commit swaps the parameters to `insert(index, element)`, matching the convention used by Ruby, JavaScript, C++, Lua, C#, Java, and Python. All documentation, C implementation, and test files are updated to reflect the new order.
2015-03-15 21:51:24 +00:00
Bob Nystrom
d731e554e6 refactor: rename debug print functions to dump and move value printing to wren_debug.c 2015-03-15 17:09:43 +00:00
Thorbjørn Lindeijer
b22040577e feat: move contains method from List to Sequence and add tests for List and Range
Add a generic `contains` implementation on the `Sequence` base class, removing the duplicate method from `List`. This allows all sequence types (including Range) to use the method without per-class redefinition. Include new test files for `List.contains` and `Range.contains` covering ordered, backwards, and exclusive ranges.
2015-03-15 14:43:10 +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
Peter Reijnders
21e387efec fix: add null and already-freed guard to wrenFreeVM to prevent double-free crash
The guard checks if the VM pointer is NULL or if its methodNames count is zero,
which serves as a heuristic to detect an already-freed VM instance. This prevents
a segmentation fault or memory corruption when wrenFreeVM is called multiple times
on the same VM object.
2015-02-28 12:29:17 +00:00