Skip to content

Conversation

zah
Copy link
Member

@zah zah commented Mar 19, 2018

Please see karaxnim/karax#55 for a working example of this.
More details can be found in the updated compiler guide.

@zah zah force-pushed the js-hot-reloading branch from fdf6132 to 31deb0a Compare March 19, 2018 11:37
@zah
Copy link
Member Author

zah commented Mar 19, 2018

One of the Travis builds has timed-out. Seems to be a false-negative.

lib/js/dom.nim Outdated
once*: bool
passive*: bool

MathLib* = ref object
Copy link
Contributor

Choose a reason for hiding this comment

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

The functions and types you added here aren't part of the DOM. They should be moved to a different module.

Copy link
Member Author

Choose a reason for hiding this comment

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

Not if you treat the DOM as the larger API surface of the browser. What would be a suitable name for this other module?

Copy link
Collaborator

Choose a reason for hiding this comment

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

In Javascript there is the Core (that is, the part of the stdlib that is requested in all environments, even outside browers), the DOM and the BOM (Browser object model). https://vkanakaraj.wordpress.com/2009/12/18/javascript-vs-dom-vs-bom-relationship-explained/

Objects such as Math, Date and so on live in the code JS library

Copy link
Contributor

Choose a reason for hiding this comment

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

jscore then?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Could make sense, although I expect most of this functionality to be available in the usual parts of the stdlib with the usual Nim names (e.g. under the modules math or times or random)

Copy link
Member Author

Choose a reason for hiding this comment

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

The point of these exposed APIs is that they allow you to call the "low-level" native methods of the JavaScript environment from jsffi-based code, which adds only a very thin layer on top of JavaScript.

Nim modules such as math and times have a slightly different goal. For all intents and purposes, they try to preserve the exact behavior of the C run-time of Nim, which often means having a thicker layer responsible for converting between the Nim and JavaScript data types and formats.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agreed. I would use jscore, dom and bom (the latter for things such as navigator, history etc. if we have it)

lib/js/jsffi.nim Outdated
## Checks, whether `x` has a property of name `prop`.

proc jsTypeOf*(x: JsObject): cstring {. importcpp: "typeof(#)" .}
proc jstypeof*(x: JsObject): cstring {. importcpp: "typeof(#)" .}
Copy link
Contributor

Choose a reason for hiding this comment

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

camelCase is valid for this case, not sure why you've changed it.

Copy link
Member Author

Choose a reason for hiding this comment

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

I made everything consistent. The other definitions were following this scheme, but I agree that the whole module could have followed NEP-1 from the start.

lib/js/jsffi.nim Outdated

template toJs*(s: string): JsObject = cstring(s).toJs

template `$`*(o: JsObject): cstring = cast[cstring](o.toString())
Copy link
Contributor

Choose a reason for hiding this comment

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

Where is toString defined? What does it return, and why is it safe to cast to a cstring?

Copy link
Member Author

Choose a reason for hiding this comment

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

o is a JsObject. toString gets compiled with the magic of the dot syntax.

Copy link
Member

Choose a reason for hiding this comment

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

This doesn't return string but cstring and so does not adhere to Nim's stdlib. Change its name to toJsString or similar.

lib/system.nim Outdated

template once*(body: untyped): untyped =
## Executes a block of code only once (the first time the block is reached).
## When code hot reloading is enabled, protects top-level code from being
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor nitpick but please s/code hot reloading/hot code reloading/

Copy link
Member Author

@zah zah Mar 23, 2018

Choose a reason for hiding this comment

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

Hrm, you are right after googlefighting the terms. I'll change that.

jsdirname* {.importc: "__dirname", nodecl.}: cstring
## JavaScript's __dirname pseudo-variable
jsfilename* {.importc: "__filename", nodecl.}: cstring
## JavaScript's __filename pseudo-variable
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd say all of the above variable names should be camel cased.


NotString = concept c
c isnot string
js* = JsObject
Copy link
Contributor

Choose a reason for hiding this comment

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

Removing js and JsRoot will cause breakage. Is this intentional? Doesn't this module depend on the definition of js?

Copy link
Member Author

Choose a reason for hiding this comment

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

This was requested by Araq.

Copy link
Contributor

Choose a reason for hiding this comment

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

Where was this requested by Araq? On IRC? Slack?

I just asked him about it via PM and he doesn't really seem sure of the reason for this request.

Copy link
Member

Choose a reason for hiding this comment

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

It was my impression that this shortcut alias was introduced in this PR secretly. My bad. Still I don't like this and we might want to deprecate it but that's not the point of this PR. Sorry! Please re-add it.

Copy link
Member Author

Choose a reason for hiding this comment

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

It was added by myself not so long ago with other fixes and additions to jsffi. It's quite likely that our own project is the only user of this short-cut.

Copy link
Contributor

Choose a reason for hiding this comment

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

That makes more sense. A separate PR for these things would be appreciated. The more unrelated changes are made in a single PR the harder it will be for us to figure out why the changes have been made in the future.

I thought this js global has been in jsffi since its initial inclusion. If it's something that was quickly added in a previous PR then it might be good to remove it, globals with such common names are usually a bad idea.

Copy link
Contributor

Choose a reason for hiding this comment

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

We are using the shortcut quite often too :(, altho nowadays we added converters to eliminate .to almost completely its still useful for js{} etc!

Copy link
Contributor

Choose a reason for hiding this comment

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

We are using the shortcut quite often too :(, altho nowadays we added converters to eliminate .to almost completely its still useful for js{} etc!

Copy link
Member Author

@zah zah Mar 24, 2018

Choose a reason for hiding this comment

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

I love this short-cut in our own code too, but Araq's argument is that it violates NEP-1 and it's easy to introduce in a project with a simple:

type js = JsObject

Copy link
Contributor

Choose a reason for hiding this comment

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

Can't we just use type Js = JsObject then to be totally NEP-1 compatible

.. code-block:: Nim
var settings = initTable[string, string]()
once:
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't it make more sense to do it the other way around? It seems like most code will have to be under this once invokation, but maybe I'm wrong.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think you are wrong, but we'll see. We already use the hot reloading branch in a quite complex project and so far this policy makes more sense.

@dom96
Copy link
Contributor

dom96 commented Mar 23, 2018

I documented the jscore module, hope you don't mind :)

@alehander92
Copy link
Contributor

Related to jscore module.

I have some local node.js/browser API type definitions(similar to jscore json) and I planned to map some of them to nim stdlib methods: does this sound useful, and where would be the best place to put them? near the existing definitions in when defined(node):, in new js lib files, something else?

@Araq
Copy link
Member

Araq commented Mar 24, 2018

  • I don't understand why once is required, seems to break the abstraction.
  • once is nothing special, what is done here is that .global vars have run-once semantics, if I'm understanding this correctly.

@alehander92
Copy link
Contributor

Is there anything blocking that? I am basing my sourcemap #7508 on that branch currently

@Araq Araq closed this Apr 13, 2018
@Araq Araq reopened this Apr 13, 2018
@Araq Araq merged commit e3037a2 into devel Apr 13, 2018
@narimiran narimiran deleted the js-hot-reloading branch October 11, 2018 06:54
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.

6 participants