Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Clean up the JSObjectRef API#28

Merged
kateinoigakukun merged 22 commits into
swiftwasm:masterswiftwasm/JavaScriptKit:masterfrom
j-f1:no-jsobject-methodsj-f1/forked-JavaScriptKit:no-jsobject-methodsCopy head branch name to clipboard
Aug 10, 2020
Merged

Clean up the JSObjectRef API#28
kateinoigakukun merged 22 commits into
swiftwasm:masterswiftwasm/JavaScriptKit:masterfrom
j-f1:no-jsobject-methodsj-f1/forked-JavaScriptKit:no-jsobject-methodsCopy head branch name to clipboard

Conversation

@j-f1

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

Copy link
Copy Markdown
Member

Fixes #27.

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

@j-f1

j-f1 commented Aug 3, 2020

Copy link
Copy Markdown
Member Author

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

j-f1 commented Aug 3, 2020

Copy link
Copy Markdown
Member Author

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

j-f1 commented Aug 3, 2020

Copy link
Copy Markdown
Member Author

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

j-f1 commented Aug 3, 2020

Copy link
Copy Markdown
Member Author

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
Copy Markdown
Member

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

@kateinoigakukun

kateinoigakukun commented Aug 4, 2020

Copy link
Copy Markdown
Member

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

j-f1 commented Aug 4, 2020

Copy link
Copy Markdown
Member Author

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

@kateinoigakukun kateinoigakukun left a comment

Copy link
Copy Markdown
Member

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.

Comment thread IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift Outdated
Comment thread Sources/JavaScriptKit/JSFunction.swift Outdated
Comment thread Sources/JavaScriptKit/JSFunction.swift Outdated
@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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.