Commit Graph

46 Commits

Author SHA1 Message Date
Bob Nystrom
701cffbe69 revert: remove safeToString_ and recursive toString handling due to memory leak on runtime errors
The safeToString_ mechanism introduced in 40897f33 used per-fiber maps and lists to detect recursive toString calls, but leaked memory when runtime errors occurred before cleanup. This reverts to direct toString implementations for List and Map, removing the recursive detection logic and updating tests to mark self-referencing containers as TODO.
2015-05-19 13:50:17 +00:00
Bob Nystrom
898ac53f4f fix: prevent stack overflow on recursive toString for List and Map
Add `String.safeToString_` static method that tracks objects currently being
converted to string per fiber, returning "..." when recursion is detected.
Wrap List.toString and Map.toString with this guard to handle self-referential
and mutually recursive structures without crashing.
2015-05-03 18:13:05 +00:00
Bob Nystrom
332f570a1a feat: add Object.same static method for reliable built-in equality
Introduce a new static method `Object.same(obj1, obj2)` that exposes the VM's
built-in equality semantics (`wrenValuesEqual`) directly, bypassing any
user-defined `==` overrides. This required creating a dedicated metaclass for
Object (subclass of Class) to host the static method without polluting every
class with a `same` method. The implementation wires up a new `Object metaclass`
in the core initialization sequence, adjusts the class hierarchy setup order,
and adds comprehensive tests covering value types, identity comparison, and
verification that `Object.same` ignores custom `==` operators.
2015-05-01 14:55:28 +00:00
Luchs
1cd91a52b4 fix: rename builtin static variables to avoid linker ambiguity across modules 2015-04-16 11:07:14 +00:00
Luchs
205bf6d596 refactor: rename static callFunction helper to callFn to avoid name collision with public API
The static helper `callFunction` in wren_core.c was renamed to `callFn` to prevent symbol conflicts with the public `wrenCallFunction` API, ensuring unique linkage and clearer separation between internal and external call paths.
2015-03-26 19:42:20 +00:00
Bob Nystrom
d92a8f1c93 fix: replace foreign Meta.eval with primitive and suppress compile errors in wrenCompile 2015-04-04 04:22:58 +00:00
Bob Nystrom
734a415999 fix: cast subtraction result to int before abs to suppress clang -Wabsolute-value warning 2015-04-04 01:38:10 +00:00
Bob Nystrom
446e8b2ca9 feat: rename Sequence.list method to toList and update all call sites
The `.list` method on Sequence was renamed to `.toList` to better reflect its
purpose of converting a sequence into a List object. This change updates the
method definition in `builtin/core.wren` and `src/vm/wren_core.c`, along with
all test files that called `.list` on sequences, maps, ranges, and custom
sequence implementations. The test file `test/core/sequence/list.wren` was also
renamed to `test/core/sequence/to_list.wren` to match the new method name.
2015-04-03 18:22:34 +00:00
Bob Nystrom
b6e2229b58 feat: move primitive class definitions from C into core.wren library source
Move the definitions of Bool, Fiber, Fn, Null, and Num classes from hardcoded C code in wren_core.c into the core.wren library source, allowing them to be parsed and created during library initialization. Simplify defineClass to always use wrenNewSingleClass, remove redundant root pushing in wrenNewClass, and adjust the class creation in the interpreter to use a Value instead of ObjClass*.
2015-04-03 15:00:55 +00:00
Bob Nystrom
17c6f277b2 refactor: merge patriciomacadden's refactoring branch into mainline
Consolidate the refactoring branch changes from patriciomacadden's fork into the main repository, incorporating the updated class definition logic in wren_core.c that replaces the separate defineSingleClass function with a unified defineClass function capable of handling both raw and subclassed class creation.
2015-04-03 14:19:17 +00:00
Luchs
3373ae0296 fix: cast subtraction to int before abs to suppress clang 3.5 -Wabsolute-value warning 2015-04-01 15:42:20 +00:00
Bob Nystrom
7884d89165 docs: rename MapSequence and WhereSequence fields and parameters for clarity, expand map(_) and where(_) docs with lazy semantics and examples 2015-04-01 14:31:15 +00:00
Bob Nystrom
85a889e49f feat: replace eager map/where with lazy MapSequence and WhereSequence classes
Refactor Sequence.map and Sequence.where to return lazy MapSequence and WhereSequence wrappers instead of eagerly building a List. Add new MapSequence and WhereSequence classes that delegate iteration and apply the transformation or predicate on-the-fly. Update documentation to reflect lazy semantics and the need to call .list to materialize results. Adjust all existing tests to call .list on map/where results. Add new tests in test/core/sequence/ verifying lazy behavior with infinite Fibonacci iterators.
2015-04-01 14:22:02 +00:00
Bob Nystrom
7d7be7361e feat: add Sequence.each method to iterate over elements with a function
Add a new `each` method to the Sequence class in the Wren programming language,
allowing iteration over sequence elements by passing each element to a provided
function. This replaces the previous pattern of using `map` with `Fiber.yield`
in examples, providing a more direct and idiomatic way to perform side effects
per element. The implementation includes a core library method in `core.wren`,
a C source string in `wren_core.c`, documentation in the Sequence markdown file,
and three test cases covering normal iteration, empty sequences, and non-function
argument error handling.
2015-04-01 14:10:21 +00:00
Bob Nystrom
07de6fca9e fix: remove extraneous space inside isspace cast in num_fromString 2015-04-01 13:43:44 +00:00
ComFreek
57919022b8 fix: cast char to unsigned char before isspace to fix char-subscripts warning 2015-03-31 16:27:44 +00:00
Patricio Mac Adden
8fdf76bb57 refactor: merge defineSingleClass into defineClass with conditional raw class creation
The defineSingleClass helper was only used for the initial Object and Class
definitions during core initialization. By inlining its logic into defineClass
with a check for vm->classClass being NULL, we eliminate a redundant function
while preserving the same behavior for those two special cases.
2015-03-29 23:18:36 +00:00
Thorbjørn Lindeijer
5b71ac0c56 feat: replace eager list-based map/where with deferred MapSequence and WhereSequence classes
Introduce MapSequence and WhereSequence subclasses of Sequence that apply transformations lazily during iteration instead of building intermediate lists. Update Sequence.map and Sequence.where to return these deferred wrappers, modify all existing tests to call .list on results, and add new infinite-sequence tests verifying lazy behavior on Fibonacci iterators.
2015-03-28 21:51:50 +00:00
Thorbjørn Lindeijer
343bfa7a84 feat: add Sequence.each method for side-effect iteration without list creation
Add a new `each` method to the Sequence class that iterates over elements
calling a provided function for each, serving as a side-effect-only
alternative to `map` when the resulting list is not needed. This is
particularly important for the upcoming change where `map` will return a
new sequence instead of mutating in place, as demonstrated by updating
the README and index examples to use `each` with Fiber.yield. Includes
documentation in the Sequence API reference and three new test cases
covering normal iteration, empty sequences, and non-function argument
error handling.
2015-03-28 19:35:20 +00:00
Bob Nystrom
540a4a166a feat: make Sequence.all() and Sequence.any() return actual predicate values instead of booleans
Rewrite all() and any() to store and return the predicate's result directly, matching && and || semantics. Update documentation to describe the new behavior. Move tests from test/core/list/ to test/core/sequence/ and add cases verifying that the first falsey/truthy value is returned.
2015-03-28 17:18:45 +00:00
Bob Nystrom
cc5bf9fcfe feat: add Sequence.list method to collect elements into a new List
Implements a new `list` method on the Sequence class that iterates over all elements and returns them as a freshly created List object. Includes documentation with usage example and a test case using a custom TestSequence class to verify correct behavior.
2015-03-28 16:06:49 +00:00
Bob Nystrom
c3d0b66630 feat: add raw byte access methods and \x escape to String class
Add `byteAt(index)`, `codePointAt(index)`, and `bytes` sequence to String,
enabling direct byte-level manipulation of UTF-8 encoded strings. Introduce
`\x` hex escape sequence in string literals for specifying raw byte values.
Refactor Unicode escape parsing into a generic `readHexEscape` function
supporting variable digit counts. Implement `wrenUtf8Decode` utility for
decoding UTF-8 sequences from byte buffers. Add comprehensive tests for
`byteAt` including boundary conditions and error cases.
2015-03-28 03:44:07 +00:00
Thorbjørn Lindeijer
33d1217d6a feat: add Sequence.list method to convert any sequence into a List
Implements a new `list` method on the Sequence class that iterates over all elements and collects them into a new List instance. Includes documentation in the core sequence docs and a test case verifying correct behavior with a custom sequence implementation.
2015-03-27 21:59:58 +00:00
Bob Nystrom
b7904a89c9 feat: add String.fromCodePoint static method and refactor UTF-8 encoding into reusable utilities
Implement the `String.fromCodePoint(_)` primitive that creates a string from a Unicode code point, with validation for integer range 0–0x10ffff. Extract inline UTF-8 encoding logic from `readUnicodeEscape` in the compiler into new `wrenUtf8NumBytes` and `wrenUtf8Encode` utility functions in `wren_utils`, and add `wrenStringFromCodePoint` helper in `wren_value`. Update documentation for core classes to add "Methods" and "Static Methods" section headers, and include `String.fromCodePoint` docs with example. Add a test file `from_code_point.wren` covering various code points.
2015-03-27 14:43:36 +00:00
Bob Nystrom
e76e466d77 feat: add Sequence.count(predicate) method and update docs/tests
Implement a new `count(f)` method on the Sequence class that iterates over elements and returns the number of items for which the given predicate function evaluates to true. Update the core library source, sequence documentation, and add three new test files covering normal usage, non-boolean return values, and non-function argument error handling. Also fix minor terminology in list docs (items → elements).
2015-03-23 14:35:17 +00:00
Bob Nystrom
15f9273aab refactor: remove unused WrenVM* parameter from buffer init, string find, and debug print functions
Remove the `WrenVM* vm` parameter from `wrenValueBufferInit`, `wrenByteBufferInit`,
`wrenIntBufferInit`, `wrenStringBufferInit`, `wrenSymbolTableInit`,
`wrenMethodBufferInit`, `wrenStringFind`, and `wrenDebugPrintStackTrace`
functions across the compiler, core, debug, utils, value, and VM modules.
Update all call sites and macro definitions to match the simplified signatures,
eliminating dead parameters that were never used within the function bodies.
2015-03-23 05:17:40 +00:00
Bob Nystrom
96a829fc8f refactor: replace ObjList manual array management with ValueBuffer in Compiler and ObjList
Replace the hand-rolled capacity/count/elements fields in ObjList with a
ValueBuffer struct, and update Compiler to use a ValueBuffer for its
constant pool instead of an ObjList. This eliminates the separate
ensureListCapacity and wrenListAdd functions, centralizing buffer growth
logic in wrenValueBufferWrite. Adjust all list primitives (add, clear,
count, iterate, iteratorValue) and the GC marking path to operate on the
embedded ValueBuffer. Add wrenMarkBuffer helper for marking buffer
contents during sweep.
2015-03-22 19:22:47 +00:00
Bob Nystrom
65305fca09 refactor: remove inline local variable declarations in list primitives and delete redundant math primitives from wren_core.c
Consolidate temporary variable usage in list_add, list_count, and list_iterate by directly inlining AS_LIST/AS_NUM calls. Remove the entire block of num_abs, num_acos, num_asin, num_atan, num_atan2, num_ceil, num_cos, num_floor, and num_fraction primitive definitions to eliminate boilerplate.
2015-03-21 21:57:43 +00:00
Bob Nystrom
d04057bf75 feat: add trigonometric methods and Num.pi constant to core library
Add acos, asin, atan, atan2, tan methods to Num class and expose Num.pi constant with value π. Include corresponding C primitives in wren_core.c and test files for each new method.
2015-03-21 19:54:08 +00:00
Bob Nystrom
52e0f4b1c0 fix: ensure step is always initialized before range validation in calculateRange 2015-03-21 19:32:31 +00:00
Bob Nystrom
07240ccb00 refactor: migrate list index and size types from int to uint32_t across core and value layers
Convert `validateIndexValue` and `validateIndex` return types to `uint32_t` with `UINT32_MAX` sentinel, update `wrenNewList`, `wrenListInsert`, `wrenListRemoveAt` signatures, and remove the TODO comment about type unification with `ObjMap`.
2015-03-21 19:22:04 +00:00
Bob Nystrom
69f135cc43 fix: change list capacity and count fields from int to uint32_t for consistency 2015-03-21 15:46:01 +00:00
Thorbjørn Lindeijer
ed559673ea feat: add Num.pi constant and trigonometric function tests for acos, asin, atan, cos, sin, tan 2015-03-19 20:12:50 +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
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
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
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
Thorbjørn Lindeijer
c717b8d3fd feat: add Num.atan(x) method mapping to C atan2 for quadrant-aware arc tangent
Adds a new `atan(_)` primitive method to the Num class that wraps the C
`atan2` function, allowing callers to compute the arc tangent of the
number divided by a given argument `x` while using the signs of both
numbers to determine the correct quadrant of the result. The
implementation registers the primitive `num_atan2` in the core VM
initialization and documents the new method in the core library
reference.
2015-03-15 15:43:02 +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
Thorbjørn Lindeijer
ac414a24fd feat: add acos, asin, atan trigonometric methods to Num class
Implement arc cosine, arc sine, and arc tangent primitives in the VM core
and document them in the Num API reference.
2015-03-14 13:52:16 +00:00
Thorbjørn Lindeijer
2e6cb23b56 feat: add Num.tan method to core library and documentation
Add the missing `tan` method to the Num class, implementing the tangent
trigonometric function. This fills the gap between the existing `sin` and
`cos` methods, providing a complete set of basic trigonometric operations.

- Register `num_tan` primitive in the VM core initialization
- Implement `DEF_PRIMITIVE(num_tan)` using C's `tan()` math function
- Add documentation entry for `tan` in the core library reference
2015-03-14 13:40:23 +00:00
Thorbjørn Lindeijer
9dc939380c feat: add Sequence.count(predicate) method and update documentation
Implement a new `count(f)` method on the Sequence class that accepts a predicate function and returns the number of elements for which the predicate evaluates to true. The implementation iterates over the sequence, calling `f.call(element)` on each element and incrementing a counter when the result is truthy.

Also update the documentation for both Sequence and List classes: add full documentation for the new `count(predicate)` method on Sequence, and change the terminology in List docs from "items" to "elements" for consistency. Add three test files covering the basic predicate behavior, non-boolean return values from the predicate, and the error case when a non-function argument is passed.
2015-03-14 13:17:21 +00:00
Peter Reijnders
875c74b211 fix: align ObjList capacity and count types with ObjMap's uint32_t
Change the `capacity` and `count` fields in `ObjList` from `int` to `uint32_t` to match the
types used in `ObjMap`, ensuring consistency between the two structures. Update all loop
variables and index calculations in `wren_core.c` and `wren_value.c` that interact with
these fields to use `uint32_t` instead of `int`, and remove the now-unnecessary negative
bounds check in `list_iterate` since unsigned types cannot be negative.
2015-03-08 11:33:29 +00:00