Comments (3)
after thinking about this more, i think it's a bad idea to keep using this pattern. it is prone to errors and results in tedious and verbose code.
an alternative is what i've called "the nursery pattern", which is the idea of having some means to keep track of newly allocated objects for a specific duration. for example, instead of:
push_value(OBJECT_VAL(string__copy(name, (int)strlen(name), vm)), vm);
push_value(OBJECT_VAL(native_function__new(function, vm)), vm);
table__set(&vm->global_vars, AS_STRING(vm->value_stack[0]), vm->value_stack[1]);
pop_value(vm);
pop_value(vm);
we might do:
memory__open_object_nursery(vm);
// e.g., `vm->nursery_start = vm->objects`
ObjectString* function_name = string__copy(name, (int)strlen(name), vm));
Value function = OBJECT_VAL(native_function__new(function, vm)));
// during a GC run (which might happen in `table__set`), we expose objects
// between `vm->nursery_start` and `vm->objects` as roots so they are not
// incorrectly disposed
table__set(&vm->global_vars, function_name, function);
memory__close_object_nursery(vm);
// e.g., `vm->nursery_start = NULL`
which is both more performant (no more pushing and popping, nor unnecessary value creation), and less error prone.
from lox.
marked as p0
due to the potential, subtle bugs that not implementing this and continuing to rely on the push/pop
pattern can bring
from lox.
#34 will fix this...
from lox.
Related Issues (20)
- make `clox` REPL be able to find `.loxrc` in more locations
- add support for bracket index access to `list` in `jlox` HOT 1
- create debugging routines in `clox`
- make `jlox` support relative paths HOT 1
- add generators to Lox
- add column number in error messages
- add `dict` class to Lox's standard library
- add `:globals` command in REPLs
- `jlox` REPL should not automatically expand `!expr` on `ENTER`
- document `help` function in `jlox` REPL
- add `assert` to the language
- figure out how to assert types generically HOT 1
- figure out whether we should emulate Python for `str::slice`
- incorrect token in error message for `list[wrong-index]`
- create error routines in `clox`
- implement a good crash trap in `clox`
- clarify distinction between "function" and "closure" in `clox`
- implement `help` function in `clox`
- Update REPL screenshots
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lox.