Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling a no-parameters function with a parameter #731

Open
andraaspar opened this issue Feb 21, 2020 · 9 comments
Open

Calling a no-parameters function with a parameter #731

andraaspar opened this issue Feb 21, 2020 · 9 comments
Labels
bug It was never supposed to do this

Comments

@andraaspar
Copy link

It seems like omitting a parameter in a function definition causes variables in the function body to take parameter values.

var f=Fn.new{
  var a=1
  System.print(a)
}
f.call(2) // 2
@andraaspar
Copy link
Author

(Not sure if this issue still exists, I'm using Wren in TIC-80 and that may be outdated. Sorry if it's old news.)

@avivbeeri
Copy link
Contributor

avivbeeri commented Feb 21, 2020

This looks like it is still present in 0.2.0

From the docs: It is a runtime error if the number of arguments given is less than the arity of the function. If more arguments are given than the function’s arity they are ignored.

Unfortunately that last part doesn't seem to be true.

@ChayimFriedman2
Copy link
Contributor

Tip: you can try the latest version of Wren in your browser at https://wren.io/try.

What you see is a bug: the arguments are stored in the stack. According to the docs, superfluous arguments should be ignored, by it seems like they're actually passed and overwrite the local variables (which are on the stack too).

@ruby0x1
Copy link
Member

ruby0x1 commented Sep 18, 2020

This is a known issue and will hopefully be addressed in 0.4.0 in some form.

@ruby0x1 ruby0x1 added the bug It was never supposed to do this label Sep 18, 2020
@ChayimFriedman2
Copy link
Contributor

Is this the same cause as the famous stack corruption issue?

@mhermier
Copy link
Contributor

mhermier commented Sep 20, 2020 via email

@ruby0x1
Copy link
Member

ruby0x1 commented Sep 25, 2020

Note that only one call type has been fixed by the other bug fix. This issue is still present on the other form.

@PureFox48
Copy link
Contributor

Note also that, whilst this bug still occurs in 0.4.0 if you pass a function to a method:

(1..1).each {
    var a 
    System.print(a) // 1 rather than null
}

thankfully, it doesn't occur with fibers:

Fiber.new {
    var a
    System.print(a) // null rather than 1
}.call(1)

This is probably because the function passed to the fiber is special in that it can take at most a single argument and you can still call it with an argument even when none is specified in order to give Fiber.yield a return value.

var f = Fiber.new {
    var a
    System.print(a) // null rather than 1
    System.print(Fiber.yield()) // 2
}

f.call(1)
f.call(2)

mhermier added a commit to mhermier/wren that referenced this issue Nov 29, 2022
`ObjFn->arity` was only populated for functions. Fix that with
wren call handles, and regular methods, so the stack can be properly
adjusted when invoking `wrenCallFunction`.
@mhermier
Copy link
Contributor

Fixed with #1124. With a new fresh view, I found it pretty quickly. The ObjFn::arity was not used in wrenCallFunction resulting in approximative ObjFiber::stackTop adjustments. Fixing that exposed an secondary issue where ObjFn::arity was not set for all the ObjFn produced by the compiler. Fixing all these resolved issue and pass all the tests.

Brugarolas added a commit to Brugarolas/wren that referenced this issue Jun 1, 2024
Populate and use `ObjFn->arity` correctly (wren-lang#731)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It was never supposed to do this
Projects
None yet
Development

No branches or pull requests

6 participants