Skip to content

Setters should return their argument #54

Open
@ChayimFriedman2

Description

@ChayimFriedman2

In Wren, assignment returns its value, like in C:

var a
System.print(a = 5) // 5

When implementing setters, we need to maintain this behavior:

// Not good
class C {
  static a=(value) {
    __a = value
  }
}
System.print(C.a = 5) // null

// Good
class C {
  static a=(value) { __a = value }
}
System.print(C.a = 5) // 5

// Also good
class C {
  static a=(value) {
    __a = value
    return value
  }
}
System.print(C.a = 5) // 5

Generally, the CLI do that, except in one foreign setter - Stdin.isRaw=(_):

wren-cli/src/module/io.c

Lines 509 to 525 in b82cf5a

void stdinIsRawSet(WrenVM* vm)
{
initStdin();
isStdinRaw = wrenGetSlotBool(vm, 1);
if (uv_guess_handle(stdinDescriptor) == UV_TTY)
{
uv_tty_t* handle = (uv_tty_t*)stdinStream;
uv_tty_set_mode(handle, isStdinRaw ? UV_TTY_MODE_RAW : UV_TTY_MODE_NORMAL);
}
else
{
// Can't set raw mode when not talking to a TTY.
// TODO: Make this a runtime error?
}
}

Instead we should do:

void stdinIsRawSet(WrenVM* vm)
{
  initStdin();
  
  isStdinRaw = wrenGetSlotBool(vm, 1);
  
  if (uv_guess_handle(stdinDescriptor) == UV_TTY)
  {
    uv_tty_t* handle = (uv_tty_t*)stdinStream;
    uv_tty_set_mode(handle, isStdinRaw ? UV_TTY_MODE_RAW : UV_TTY_MODE_NORMAL);
  }
  else
  {
    // Can't set raw mode when not talking to a TTY.
    // TODO: Make this a runtime error?
  }

  wrenEnsureSlots(vm, 1); // Is that required? Probably not
  wrenSetSlotBool(vm, 0, isStdinRaw);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions