{"action":"created","comment":{"url":"https://api.github.com/repos/WebAssembly/esm-integration/pulls/comments/230306764","pull_request_review_id":171015464,"id":230306764,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIzMDMwNjc2NA==","diff_hunk":"@@ -88,57 +88,74 @@ Three things happen for each module during the Construction phase.\n \n ### Instantiation\n \n-During instantiation, exports and imports are wired up to each other. In JS, functions are initialized at this point. All other values are undefined until evaluation.\n+When `Module.Instantiate` is called on a JavaScript module, a Lexical Environment is set up. Space for exports is allocated and the memory locations are bound to the export name. Additionally, any imports from dependencies are resolved. In JS, function declarations are initialized at this point. All other values are undefined or in [TDZ](http://2ality.com/2015/10/why-tdz.html) (i.e. would lead to a ReferenceError on access) until evaluation.\n \n-For WebAssembly, instantiation will include initialization for all [external types](https://webassembly.github.io/spec/core/exec/modules.html#external-typing). External types currently include:\n+When `Module.Instantiate` is called on a WebAssembly module, the module will go through the same process. A Lexical Environment is set up, and all exports (for functions, tables, memories and globals) have a binding created for them, which is initialized to TDZ. This process is not WebAssembly module instantiation--WebAssembly validation is not run at this point, and the start function is not executed. Unlike in JavaScript, functions will not be initialized at this point. Function exports, like other exports, start out as TDZ. Initialization of these bindings to take them out of TDZ for all values happens during evaluation.\n+\n+WebAssembly exports can be any one of the [external types](https://webassembly.github.io/spec/core/exec/modules.html#external-typing), which currently include:\n \n - functions\n - tables\n - memories\n - globals\n \n-Because WebAssembly does not currently have a type that can handle arbitrary JS values, JS value imports can not be handled with live bindings and would be snapshotted when they are imported. This means that in the short term, non-function imports coming from JS modules would be assigned a value of `undefined`, which will currently result in an error. \n+WebAssembly does not currently have a type that can handle arbitrary JS values. This means that JS value imports can not be handled with live bindings and will be snapshotted when they are imported. This means that if a JS module changes one of its exports to point to a different object, a WebAssembly module importing that object will not see the change.\n \n-It may be possible to provide live bindings for JS values if an `any` type is added to WebAssembly. At this point the restriction prohibiting `undefined` imports could also be lifted. However, we would still want to throw an error for non-function imports in this case. \n+It may be possible to provide live bindings for JS values if an [`anyref`](https://github.com/WebAssembly/reference-types) type is added to WebAssembly.\n \n-The reason we want to throw an error in this case is two fold:\n+### Evaluation\n \n-- **Reduce confusion for developers.** Since non-function imports work in JS->JS dependency chains, it is unintuitive that they would be undefined in JS->wasm dependency chains. Throwing an error makes clear what's happening.\n-- **Allow for future support.** It may be possible to support non-function imports to wasm modules if an `any` type is added. We close off this avenue if we allow users to import values that can be undefined, though, as code may start depending on that behavior.\n+During evaluation, the code is evaluated to assign values to the exported bindings. In JS, this means running the top-level module code.\n \n-One important limitation of this approach to instantiation is that function imports won't work in JS in JS<->wasm cycles where the JS module is higher in the graph. In that case, the WebAssembly module is instantiated before the JS module, which means that functions haven't been initialized and would throw the same error as non-function imports. \n+WebAssembly has explicit types for both imports and exports. The type of exports is used to populate the lexical environment with a JavaScript value based on the WebAssembly export, from the [WebAssembly JS API](https://webassembly.github.io/spec/js-api/index.html). The type of imports is used to type check or convert imports to the required WebAssembly type.\n \n-Because of this, instantiation may need to be split into two separate sub-phases for WebAssembly. In this approach, the current instantiation algorithm would be run in the first sub-phase, ensuring that all JS functions are initialized. Then, in the second sub-phase, WebAssembly would be able to snapshot these function exports.\n+For WebAssembly, evaluation consists of:\n+- validating the WebAssembly module\n+- snapshotting all imports:\n+    - If any import is in TDZ, throw a ReferenceError exception\n+    - Type-check/coerce each imported value against the declared import type, per step 8 of the [*instantiate a WebAssembly module*](https://webassembly.github.io/spec/js-api/index.html#instantiate-a-webassembly-module) algorithm.\n+    - Initialize (\"snapshot\") the imported bindings to the checked/coerced value\n+- initializing the Wasm module, performing [Wasm module instantiation](https://webassembly.github.io/spec/core/appendix/embedding.html#embed-instantiate-module)\n+    - filling in memory using the data segment\n+    - filling in tables using elem segments\n+    - running the start function, which corresponds to running the top-level module code\n+- initializing all exports in the Lexical Environment to their analogous Wasm/JS API objects, per step 4 of the [*instantiate a WebAssembly module*](https://webassembly.github.io/spec/js-api/index.html#instantiate-a-webassembly-module) algorithm.\n \n-### Evaluation\n+Because JS modules that the WebAssembly module imports from are already evaluated at this point, their values will be available for WebAssembly to snapshot (as long as the WebAssembly module was not in a cycle with the JS module, with the WebAssembly module's evaluation preceding the JS module's evaluation).\n \n-During evaluation, the code is evaluated to assign values to the exported bindings. In JS, this means running the top-level module code.\n+#### A note on snapshotting\n \n-For WebAssembly, evaluation consists of:\n-- filling in memory using the data segment\n-- filling in tables using elem segments\n-- running the start function, which corresponds to running the top-level module code\n+For JavaScript ES modules, an export name is bound to a slot on the Module Record. For all of the values that we currently allow, this slot will contain a reference to another memory location, which contains the object (e.g. the `WebAssembly.Global` object that is being imported).\n+\n+The term \"snapshot\" means that WebAssembly will snapshot the reference. This does result in an observable difference between the way JavaScript and WebAssembly handle updates. In JavaScript modules, it is possible for the exporting module to update an export to point to a different object. Other JavaScript modules that are importing that export will then get the new object. In contrast, while WebAssembly modules will see changes to the object that the export was first bound to (e.g. when the exporting module calls `WebAssembly.Global.prototype.set` to change the value), the WebAssembly module will never see updated bindings.","path":"proposals/esm-integration/README.md","position":58,"original_position":58,"commit_id":"d75684286bcfa07cb74e87a3e6bb047ffa1a4ae7","original_commit_id":"d75684286bcfa07cb74e87a3e6bb047ffa1a4ae7","user":{"login":"littledan","id":189835,"node_id":"MDQ6VXNlcjE4OTgzNQ==","avatar_url":"https://avatars3.githubusercontent.com/u/189835?v=4","gravatar_id":"","url":"https://api.github.com/users/littledan","html_url":"https://github.com/littledan","followers_url":"https://api.github.com/users/littledan/followers","following_url":"https://api.github.com/users/littledan/following{/other_user}","gists_url":"https://api.github.com/users/littledan/gists{/gist_id}","starred_url":"https://api.github.com/users/littledan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/littledan/subscriptions","organizations_url":"https://api.github.com/users/littledan/orgs","repos_url":"https://api.github.com/users/littledan/repos","events_url":"https://api.github.com/users/littledan/events{/privacy}","received_events_url":"https://api.github.com/users/littledan/received_events","type":"User","site_admin":false},"body":"I don't quite understand--the point of this paragraph is to work through that confusion and explain what the semantics are.","created_at":"2018-11-02T09:02:32Z","updated_at":"2018-11-02T09:02:32Z","html_url":"https://github.com/WebAssembly/esm-integration/pull/21#discussion_r230306764","pull_request_url":"https://api.github.com/repos/WebAssembly/esm-integration/pulls/21","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/WebAssembly/esm-integration/pulls/comments/230306764"},"html":{"href":"https://github.com/WebAssembly/esm-integration/pull/21#discussion_r230306764"},"pull_request":{"href":"https://api.github.com/repos/WebAssembly/esm-integration/pulls/21"}},"in_reply_to_id":226832415},"pull_request":{"url":"https://api.github.com/repos/WebAssembly/esm-integration/pulls/21","id":224328704,"node_id":"MDExOlB1bGxSZXF1ZXN0MjI0MzI4NzA0","html_url":"https://github.com/WebAssembly/esm-integration/pull/21","diff_url":"https://github.com/WebAssembly/esm-integration/pull/21.diff","patch_url":"https://github.com/WebAssembly/esm-integration/pull/21.patch","issue_url":"https://api.github.com/repos/WebAssembly/esm-integration/issues/21","number":21,"state":"open","locked":false,"title":"Early draft specification; modified explainer","user":{"login":"littledan","id":189835,"node_id":"MDQ6VXNlcjE4OTgzNQ==","avatar_url":"https://avatars3.githubusercontent.com/u/189835?v=4","gravatar_id":"","url":"https://api.github.com/users/littledan","html_url":"https://github.com/littledan","followers_url":"https://api.github.com/users/littledan/followers","following_url":"https://api.github.com/users/littledan/following{/other_user}","gists_url":"https://api.github.com/users/littledan/gists{/gist_id}","starred_url":"https://api.github.com/users/littledan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/littledan/subscriptions","organizations_url":"https://api.github.com/users/littledan/orgs","repos_url":"https://api.github.com/users/littledan/repos","events_url":"https://api.github.com/users/littledan/events{/privacy}","received_events_url":"https://api.github.com/users/littledan/received_events","type":"User","site_admin":false},"body":"Spec rendering: https://littledan.github.io/esm-integration/#esm-integration\r\nNew explainer: https://github.com/littledan/esm-integration/blob/master/proposals/esm-integration/README.md\r\n\r\nFixes #16, #18, #19","created_at":"2018-10-19T15:35:57Z","updated_at":"2018-11-02T09:02:32Z","closed_at":null,"merged_at":null,"merge_commit_sha":"9be38d76508799dcaf1a2b8ce3ee7f99f1b316ef","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/WebAssembly/esm-integration/pulls/21/commits","review_comments_url":"https://api.github.com/repos/WebAssembly/esm-integration/pulls/21/comments","review_comment_url":"https://api.github.com/repos/WebAssembly/esm-integration/pulls/comments{/number}","comments_url":"https://api.github.com/repos/WebAssembly/esm-integration/issues/21/comments","statuses_url":"https://api.github.com/repos/WebAssembly/esm-integration/statuses/d75684286bcfa07cb74e87a3e6bb047ffa1a4ae7","head":{"label":"littledan:type-update-revision","ref":"type-update-revision","sha":"d75684286bcfa07cb74e87a3e6bb047ffa1a4ae7","user":{"login":"littledan","id":189835,"node_id":"MDQ6VXNlcjE4OTgzNQ==","avatar_url":"https://avatars3.githubusercontent.com/u/189835?v=4","gravatar_id":"","url":"https://api.github.com/users/littledan","html_url":"https://github.com/littledan","followers_url":"https://api.github.com/users/littledan/followers","following_url":"https://api.github.com/users/littledan/following{/other_user}","gists_url":"https://api.github.com/users/littledan/gists{/gist_id}","starred_url":"https://api.github.com/users/littledan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/littledan/subscriptions","organizations_url":"https://api.github.com/users/littledan/orgs","repos_url":"https://api.github.com/users/littledan/repos","events_url":"https://api.github.com/users/littledan/events{/privacy}","received_events_url":"https://api.github.com/users/littledan/received_events","type":"User","site_admin":false},"repo":{"id":153156089,"node_id":"MDEwOlJlcG9zaXRvcnkxNTMxNTYwODk=","name":"esm-integration","full_name":"littledan/esm-integration","private":false,"owner":{"login":"littledan","id":189835,"node_id":"MDQ6VXNlcjE4OTgzNQ==","avatar_url":"https://avatars3.githubusercontent.com/u/189835?v=4","gravatar_id":"","url":"https://api.github.com/users/littledan","html_url":"https://github.com/littledan","followers_url":"https://api.github.com/users/littledan/followers","following_url":"https://api.github.com/users/littledan/following{/other_user}","gists_url":"https://api.github.com/users/littledan/gists{/gist_id}","starred_url":"https://api.github.com/users/littledan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/littledan/subscriptions","organizations_url":"https://api.github.com/users/littledan/orgs","repos_url":"https://api.github.com/users/littledan/repos","events_url":"https://api.github.com/users/littledan/events{/privacy}","received_events_url":"https://api.github.com/users/littledan/received_events","type":"User","site_admin":false},"html_url":"https://github.com/littledan/esm-integration","description":"ECMAScript module integration","fork":true,"url":"https://api.github.com/repos/littledan/esm-integration","forks_url":"https://api.github.com/repos/littledan/esm-integration/forks","keys_url":"https://api.github.com/repos/littledan/esm-integration/keys{/key_id}","collaborators_url":"https://api.github.com/repos/littledan/esm-integration/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/littledan/esm-integration/teams","hooks_url":"https://api.github.com/repos/littledan/esm-integration/hooks","issue_events_url":"https://api.github.com/repos/littledan/esm-integration/issues/events{/number}","events_url":"https://api.github.com/repos/littledan/esm-integration/events","assignees_url":"https://api.github.com/repos/littledan/esm-integration/assignees{/user}","branches_url":"https://api.github.com/repos/littledan/esm-integration/branches{/branch}","tags_url":"https://api.github.com/repos/littledan/esm-integration/tags","blobs_url":"https://api.github.com/repos/littledan/esm-integration/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/littledan/esm-integration/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/littledan/esm-integration/git/refs{/sha}","trees_url":"https://api.github.com/repos/littledan/esm-integration/git/trees{/sha}","statuses_url":"https://api.github.com/repos/littledan/esm-integration/statuses/{sha}","languages_url":"https://api.github.com/repos/littledan/esm-integration/languages","stargazers_url":"https://api.github.com/repos/littledan/esm-integration/stargazers","contributors_url":"https://api.github.com/repos/littledan/esm-integration/contributors","subscribers_url":"https://api.github.com/repos/littledan/esm-integration/subscribers","subscription_url":"https://api.github.com/repos/littledan/esm-integration/subscription","commits_url":"https://api.github.com/repos/littledan/esm-integration/commits{/sha}","git_commits_url":"https://api.github.com/repos/littledan/esm-integration/git/commits{/sha}","comments_url":"https://api.github.com/repos/littledan/esm-integration/comments{/number}","issue_comment_url":"https://api.github.com/repos/littledan/esm-integration/issues/comments{/number}","contents_url":"https://api.github.com/repos/littledan/esm-integration/contents/{+path}","compare_url":"https://api.github.com/repos/littledan/esm-integration/compare/{base}...{head}","merges_url":"https://api.github.com/repos/littledan/esm-integration/merges","archive_url":"https://api.github.com/repos/littledan/esm-integration/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/littledan/esm-integration/downloads","issues_url":"https://api.github.com/repos/littledan/esm-integration/issues{/number}","pulls_url":"https://api.github.com/repos/littledan/esm-integration/pulls{/number}","milestones_url":"https://api.github.com/repos/littledan/esm-integration/milestones{/number}","notifications_url":"https://api.github.com/repos/littledan/esm-integration/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/littledan/esm-integration/labels{/name}","releases_url":"https://api.github.com/repos/littledan/esm-integration/releases{/id}","deployments_url":"https://api.github.com/repos/littledan/esm-integration/deployments","created_at":"2018-10-15T17:41:53Z","updated_at":"2018-10-19T15:33:20Z","pushed_at":"2018-10-19T15:32:23Z","git_url":"git://github.com/littledan/esm-integration.git","ssh_url":"git@github.com:littledan/esm-integration.git","clone_url":"https://github.com/littledan/esm-integration.git","svn_url":"https://github.com/littledan/esm-integration","homepage":"https://littledan.github.io/esm-integration/","size":8075,"stargazers_count":0,"watchers_count":0,"language":"WebAssembly","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"other","name":"Other","spdx_id":"NOASSERTION","url":null,"node_id":"MDc6TGljZW5zZTA="},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"WebAssembly:master","ref":"master","sha":"9cf6ea9db809f970e406773bb16ece1359aa9464","user":{"login":"WebAssembly","id":11578470,"node_id":"MDEyOk9yZ2FuaXphdGlvbjExNTc4NDcw","avatar_url":"https://avatars0.githubusercontent.com/u/11578470?v=4","gravatar_id":"","url":"https://api.github.com/users/WebAssembly","html_url":"https://github.com/WebAssembly","followers_url":"https://api.github.com/users/WebAssembly/followers","following_url":"https://api.github.com/users/WebAssembly/following{/other_user}","gists_url":"https://api.github.com/users/WebAssembly/gists{/gist_id}","starred_url":"https://api.github.com/users/WebAssembly/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/WebAssembly/subscriptions","organizations_url":"https://api.github.com/users/WebAssembly/orgs","repos_url":"https://api.github.com/users/WebAssembly/repos","events_url":"https://api.github.com/users/WebAssembly/events{/privacy}","received_events_url":"https://api.github.com/users/WebAssembly/received_events","type":"Organization","site_admin":false},"repo":{"id":125050425,"node_id":"MDEwOlJlcG9zaXRvcnkxMjUwNTA0MjU=","name":"esm-integration","full_name":"WebAssembly/esm-integration","private":false,"owner":{"login":"WebAssembly","id":11578470,"node_id":"MDEyOk9yZ2FuaXphdGlvbjExNTc4NDcw","avatar_url":"https://avatars0.githubusercontent.com/u/11578470?v=4","gravatar_id":"","url":"https://api.github.com/users/WebAssembly","html_url":"https://github.com/WebAssembly","followers_url":"https://api.github.com/users/WebAssembly/followers","following_url":"https://api.github.com/users/WebAssembly/following{/other_user}","gists_url":"https://api.github.com/users/WebAssembly/gists{/gist_id}","starred_url":"https://api.github.com/users/WebAssembly/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/WebAssembly/subscriptions","organizations_url":"https://api.github.com/users/WebAssembly/orgs","repos_url":"https://api.github.com/users/WebAssembly/repos","events_url":"https://api.github.com/users/WebAssembly/events{/privacy}","received_events_url":"https://api.github.com/users/WebAssembly/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/WebAssembly/esm-integration","description":"ECMAScript module integration","fork":false,"url":"https://api.github.com/repos/WebAssembly/esm-integration","forks_url":"https://api.github.com/repos/WebAssembly/esm-integration/forks","keys_url":"https://api.github.com/repos/WebAssembly/esm-integration/keys{/key_id}","collaborators_url":"https://api.github.com/repos/WebAssembly/esm-integration/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/WebAssembly/esm-integration/teams","hooks_url":"https://api.github.com/repos/WebAssembly/esm-integration/hooks","issue_events_url":"https://api.github.com/repos/WebAssembly/esm-integration/issues/events{/number}","events_url":"https://api.github.com/repos/WebAssembly/esm-integration/events","assignees_url":"https://api.github.com/repos/WebAssembly/esm-integration/assignees{/user}","branches_url":"https://api.github.com/repos/WebAssembly/esm-integration/branches{/branch}","tags_url":"https://api.github.com/repos/WebAssembly/esm-integration/tags","blobs_url":"https://api.github.com/repos/WebAssembly/esm-integration/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/WebAssembly/esm-integration/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/WebAssembly/esm-integration/git/refs{/sha}","trees_url":"https://api.github.com/repos/WebAssembly/esm-integration/git/trees{/sha}","statuses_url":"https://api.github.com/repos/WebAssembly/esm-integration/statuses/{sha}","languages_url":"https://api.github.com/repos/WebAssembly/esm-integration/languages","stargazers_url":"https://api.github.com/repos/WebAssembly/esm-integration/stargazers","contributors_url":"https://api.github.com/repos/WebAssembly/esm-integration/contributors","subscribers_url":"https://api.github.com/repos/WebAssembly/esm-integration/subscribers","subscription_url":"https://api.github.com/repos/WebAssembly/esm-integration/subscription","commits_url":"https://api.github.com/repos/WebAssembly/esm-integration/commits{/sha}","git_commits_url":"https://api.github.com/repos/WebAssembly/esm-integration/git/commits{/sha}","comments_url":"https://api.github.com/repos/WebAssembly/esm-integration/comments{/number}","issue_comment_url":"https://api.github.com/repos/WebAssembly/esm-integration/issues/comments{/number}","contents_url":"https://api.github.com/repos/WebAssembly/esm-integration/contents/{+path}","compare_url":"https://api.github.com/repos/WebAssembly/esm-integration/compare/{base}...{head}","merges_url":"https://api.github.com/repos/WebAssembly/esm-integration/merges","archive_url":"https://api.github.com/repos/WebAssembly/esm-integration/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/WebAssembly/esm-integration/downloads","issues_url":"https://api.github.com/repos/WebAssembly/esm-integration/issues{/number}","pulls_url":"https://api.github.com/repos/WebAssembly/esm-integration/pulls{/number}","milestones_url":"https://api.github.com/repos/WebAssembly/esm-integration/milestones{/number}","notifications_url":"https://api.github.com/repos/WebAssembly/esm-integration/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/WebAssembly/esm-integration/labels{/name}","releases_url":"https://api.github.com/repos/WebAssembly/esm-integration/releases{/id}","deployments_url":"https://api.github.com/repos/WebAssembly/esm-integration/deployments","created_at":"2018-03-13T12:46:59Z","updated_at":"2018-10-25T11:50:01Z","pushed_at":"2018-10-19T15:35:58Z","git_url":"git://github.com/WebAssembly/esm-integration.git","ssh_url":"git@github.com:WebAssembly/esm-integration.git","clone_url":"https://github.com/WebAssembly/esm-integration.git","svn_url":"https://github.com/WebAssembly/esm-integration","homepage":null,"size":8033,"stargazers_count":40,"watchers_count":40,"language":"WebAssembly","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":7,"mirror_url":null,"archived":false,"open_issues_count":12,"license":{"key":"other","name":"Other","spdx_id":"NOASSERTION","url":null,"node_id":"MDc6TGljZW5zZTA="},"forks":7,"open_issues":12,"watchers":40,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/WebAssembly/esm-integration/pulls/21"},"html":{"href":"https://github.com/WebAssembly/esm-integration/pull/21"},"issue":{"href":"https://api.github.com/repos/WebAssembly/esm-integration/issues/21"},"comments":{"href":"https://api.github.com/repos/WebAssembly/esm-integration/issues/21/comments"},"review_comments":{"href":"https://api.github.com/repos/WebAssembly/esm-integration/pulls/21/comments"},"review_comment":{"href":"https://api.github.com/repos/WebAssembly/esm-integration/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/WebAssembly/esm-integration/pulls/21/commits"},"statuses":{"href":"https://api.github.com/repos/WebAssembly/esm-integration/statuses/d75684286bcfa07cb74e87a3e6bb047ffa1a4ae7"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"littledan"}}