-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Support code hot reloading for JavaScript projects #7362
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
Conversation
…oesn't follow NEP-1
|
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jscore then?
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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(#)" .} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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/
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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 = JsObjectThere was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
I documented the |
|
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 |
|
|
Is there anything blocking that? I am basing my sourcemap #7508 on that branch currently |
Please see karaxnim/karax#55 for a working example of this.
More details can be found in the updated compiler guide.