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

Invoking constructors changes global this instead of newTarget #1009

Closed
tofpie opened this issue Dec 29, 2020 · 2 comments · Fixed by #1045
Closed

Invoking constructors changes global this instead of newTarget #1009

tofpie opened this issue Dec 29, 2020 · 2 comments · Fixed by #1045
Assignees
Labels
bug Something isn't working builtins PRs and Issues related to builtins/intrinsics
Milestone

Comments

@tofpie
Copy link
Contributor

tofpie commented Dec 29, 2020

Describe the bug
String used as a function call messes up with the global object.

To Reproduce

This JavaScript code reproduces the issue:

String(10)
this.length
String.prototype.toString.call(this)

Running this code, 2 is printed as this.length and "10" is printed as String.prototype.toString.call(this).

Expected behavior
Running this code, this.length should stay undefined and String.prototype.toString.call(this) should throw a TypeError.

Build environment (please complete the following information):

  • OS: Windows 10
  • Version: 0.10
  • Target triple: x86_64-pc-windows-msvc
  • Rustc version: rustc 1.48.0 (7eac88abb 2020-11-16)

Additional context
None

@tofpie tofpie added the bug Something isn't working label Dec 29, 2020
@tofpie
Copy link
Contributor Author

tofpie commented Dec 29, 2020

This is this part that messes up with the global object when String is called directly:

pub(crate) fn constructor(
this: &Value,
args: &[Value],
context: &mut Context,
) -> Result<Value> {
// This value is used by console.log and other routines to match Obexpecty"failed to parse argument for String method"pe
// to its Javascript Identifier (global constructor method name)
let string = match args.get(0) {
Some(ref value) => value.to_string(context)?,
None => RcString::default(),
};
let length = DataDescriptor::new(
Value::from(string.encode_utf16().count()),
Attribute::NON_ENUMERABLE,
);
this.set_property("length", length);
this.set_data(ObjectData::String(string.clone()));
Ok(Value::from(string))
}

But I really do not know how to fix that. If anyone has some guidance, I can give it a try though.

@RageKnify
Copy link
Contributor

I think this can happen with most builtin constructors, we can generate a similar effect with:

>>  Boolean()
>> this
Boolean { false }

I assume we need to change most of the constructors, the spec mentions newTarget, which I think can be found in boa in

rust-analyzer only spots it only as being declared and initialized so it seems it currently isn't used.

@RageKnify RageKnify added the builtins PRs and Issues related to builtins/intrinsics label Dec 29, 2020
@RageKnify RageKnify changed the title String used as a function call messes up with the global object Invoking constructors changes global this instead of newTarget Dec 29, 2020
@Razican Razican added this to the v0.11.0 milestone Jan 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working builtins PRs and Issues related to builtins/intrinsics
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants