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

Clean up the JSObjectRef API #28

Merged
merged 22 commits into from
Aug 10, 2020

Conversation

j-f1
Copy link
Member

@j-f1 j-f1 commented Aug 3, 2020

Fixes #27.

  • get/setdynamicMember and subscripts
  • instanceof (kept)
  • jsValue (kept)
  • applycallAsFunction

@j-f1
Copy link
Member Author

j-f1 commented Aug 3, 2020

Inspired by the ExpressibleByNilLiteral protocol, I came up with this subscript-based JSValueConvertible protocol:

public protocol JSValueConvertible {
    subscript(jsValue _: ()) -> JSValue { get }
}

extension JSValue {
    public init(from convertible: JSValueConvertible) {
        self = convertible[jsValue: ()]
    }
}

@j-f1 j-f1 marked this pull request as ready for review August 3, 2020 14:39
@j-f1
Copy link
Member Author

j-f1 commented Aug 3, 2020

Alright, the tests are still failing because it seems like I can’t have Array conform to JSValueConvertible when either Element: JSValueConvertible or Element == JSValueConvertible :(

Any suggestions?

@j-f1
Copy link
Member Author

j-f1 commented Aug 3, 2020

Update: I’ve replaced all the function call/new logic with this pattern:

// idiomatic JS version on next line
foo()
foo()

foo(arg1, arg2, arg3)
foo(arg1, arg2, arg3)

foo(args: [arg1, arg2, arg3])
foo(...[arg1, arg2, arg3])

foo(this: bar)
foo.call(bar)

foo(this: bar, arg1, arg2, arg3)
foo.call(bar, arg1, arg2, arg3)

foo(this: bar, args: [arg1, arg2, arg3])
foo.apply(bar,  [arg1, arg2, arg3])

foo(new: arg1, arg2, arg3)
new foo(arg1, arg2, arg3)

foo(.new, args: [arg1, arg2, arg3])
new foo(...[arg1, arg2, arg3])

foo(.new)
new foo()

The last two I’m a little uncertain of.

@j-f1
Copy link
Member Author

j-f1 commented Aug 3, 2020

One last change I was going to make was changing the instanceof function to an operator. I tried it out with << which isn’t super clear.

It seems Swift doesn’t allow letters in operators, so instanceof is out. There are a ton of Unicode characters allowed but the only ASCII ones are !%&*+-/<=>?^|~. Maybe <<? or <==?

Screenshot_2020-08-03 14 04 40

@kateinoigakukun
Copy link
Member

Sorry, I didn't take a look much now, but could you ignore .swiftpm directory?

@kateinoigakukun
Copy link
Member

kateinoigakukun commented Aug 4, 2020

Sorry for late response. I basically agree with deprecating get and set and provide subscript.

But on second thought, new and instanceof are not natural and not swifty interface.

This library provides JavaScript like APIs but also they should be swifty.

I think instanceof, new and jsValue are not common naming as members and if users want to use those names, they can use subscript accessor instead.

I disagree that APIs become unnatural caring about edge cases.

@j-f1
Copy link
Member Author

j-f1 commented Aug 4, 2020

Good point! I reverted those changes, so now the last few samples look like this:

foo.new(arg1, arg2, arg3)
new foo(arg1, arg2, arg3)

foo.new(args: [arg1, arg2, arg3])
new foo(...[arg1, arg2, arg3])

foo.new()
new foo()

Additionally, I changed the JSValue code back to using .jsValue() and changed instanceof to add the method isInstanceOf.

Ah, did a quick GitHub search and isInstanceOf seems to be somewhat common. Should I switch back to the slightly-less-readable name option instanceof?

@j-f1 j-f1 changed the title Remove all methods from JSObjects to free up the property namespace Clean up the JSObjectRef API Aug 4, 2020
Copy link
Member

@kateinoigakukun kateinoigakukun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that isInstanceOf member name is used by only some testing frameworks, so I think it's ok to keep isInstanceOf.

@kateinoigakukun kateinoigakukun merged commit de61e95 into swiftwasm:master Aug 10, 2020
@j-f1 j-f1 deleted the no-jsobject-methods branch August 10, 2020 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Separate namespaces for methods and properties?
2 participants