wren-lang / wren

The Wren Programming Language. Wren is a small, fast, class-based concurrent scripting language.
http://wren.io
MIT License
6.9k stars 552 forks source link

New C API `wrenCopySlot()` #836

Open ChayimFriedman2 opened 3 years ago

ChayimFriedman2 commented 3 years ago

In short, we need a C function to copy one slot to another, not matter what it contains. The implementation in C is straightforward.

Why?

Because setters, that's why.

Setters should return their argument in order to keep Wren assignment semantics. See https://github.com/wren-lang/wren-cli/issues/54 for a broader explanation.

Edit: To clarify, setters should return the assigned value, i.e. the value after transformations was applied. This does not matter most of the cases though.

Setters not always know their arguments. For foreign setters to be correctly implemented, we need now to switch the wrenGetSlotType() and use the appropriate method. Ugh. A wrenCopySlot() will be much better.

mhermier commented 3 years ago

Argumentation is wrong, setters should always returns this, and it is the default case. But there is a real potential usage because sometime you need to create an object and conserve/copy some slots to populate the created object, before returning the created object.

Instead of a single item copy, I would go wrenCopySlots(int dstSlot, int srcSlot, size_t size) instead.

There is also a potential need for 2 functions to transfer the stack to/from an object. It can be required when creating/unwinding objects.

ChayimFriedman2 commented 3 years ago

I agree with your argument, but don't agree with your thought about setters. It is very confusing if a = 1 returns 1 but a.b = 1 returns a. Also that means that anyway, we need to change (almost) all setters in Wren and the CLI - in the CLI they're one-line, and in the VM it's even explicit when they return their 2nd argument. I covered them one by one - all of them do the same.

mhermier commented 3 years ago

I rethought about it, and was wrong, and you are almost right. Strictly speaking, it should returns the value held, which is usually the value passed. But there might be cases were the value is transformed, and it should returns that value instead.

ChayimFriedman2 commented 3 years ago

@mhermier I completely agree, and that was what I meant. I'll edit to clarify. The reason I haven't mentioned this is that in the cases the value is transformed, we don't need the new API 😄.

ChayimFriedman2 commented 3 years ago

Another motivation, thanks to cagey from discord: Passing an instance to Wren function. The instance has been returned by the constructor in slot 0, but has to be put in another slot. The handles API handles that.