From f23c39af1b1a4b60b47ad3f26998762cf24e445c Mon Sep 17 00:00:00 2001 From: codehag Date: Wed, 23 May 2018 16:14:28 -0400 Subject: [PATCH 1/5] attempt to define brand check --- terminology.md | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/terminology.md b/terminology.md index 175c657..7bd974d 100644 --- a/terminology.md +++ b/terminology.md @@ -33,6 +33,58 @@ name should be. We should avoid such bikeshedding. #### Sources [wikipedia](https://en.wiktionary.org/wiki/bikeshedding): +### Brand Check + +#### Definition: + +Brand check ("brand" as in a mark, or a brand made with a branding iron) is a term used by the TC39 +to describe a check against a unique datatype whose createion is controlled by a piece of code. + +#### Example: + +One example of this is built in JavaScript datatypes, which are unique and cannot be made in user +space. For example, `toString` can be used on the `new Date` object, and this is a unique identifier +which returns `[object Date]`. For reference see [this +discussion](https://esdiscuss.org/topic/tostringtag-spoofing-for-null-and-undefined#content-3) + +However, this is not limited to datatypes that are implemented as a part of JavaScript. Brand checks +are possible in user space as long as there is a way to identify that the object is unique. + +Imagine a library that implements dom queries and returns a `query` object. The author of this +library may be interested in being able to modify the implementation of the `query` object without +breaking the programs of users of the library. However, returning plain objects such as ` { type: +"queryResult", elements: [ ...... ] }` is not safe, as anyone can return such an object and create a +forgery of a `query` object. In order to avoid this, the library must make a brand check to ensure +that this object indeed belongs to the library. That can be done like so: + +```javascript +const queries = new WeakMap(); + +class Query { + // ... + + performQuery(queryString { + // returns the query object + return { type: "queryResult", elements: [ ...... ] }; + } + + get query(query) { + queries.get(query); // verifies that the query exists as a member of the WeakMap + } + + set query(queryString) { + // generate a query object + const query = performQuery(queryString); + // use the object itself as the key + queries.set(query, ...); + } +} +``` + + +#### Sources +[esdiscuss comment](https://esdiscuss.org/topic/tostringtag-spoofing-for-null-and-undefined#content-3) + ### Temporal dead zone (TDZ) #### Definition: From 4e5666e7b7b50013d2f2ab7d0e84eac1dad55edb Mon Sep 17 00:00:00 2001 From: codehag Date: Fri, 29 Jun 2018 14:08:14 +0200 Subject: [PATCH 2/5] update pr to have links to discussion --- terminology.md | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/terminology.md b/terminology.md index 7bd974d..26acbe5 100644 --- a/terminology.md +++ b/terminology.md @@ -38,19 +38,22 @@ name should be. We should avoid such bikeshedding. #### Definition: Brand check ("brand" as in a mark, or a brand made with a branding iron) is a term used by the TC39 -to describe a check against a unique datatype whose createion is controlled by a piece of code. +to describe a check against a unique datatype whose creation is controlled by a piece of code. #### Example: One example of this is built in JavaScript datatypes, which are unique and cannot be made in user -space. For example, `toString` can be used on the `new Date` object, and this is a unique identifier -which returns `[object Date]`. For reference see [this +space. `Array.isArray` is an example of a brand check. For reference see [this discussion](https://esdiscuss.org/topic/tostringtag-spoofing-for-null-and-undefined#content-3) -However, this is not limited to datatypes that are implemented as a part of JavaScript. Brand checks -are possible in user space as long as there is a way to identify that the object is unique. +A common misconception is that `instanceof` is a brand check. This is a nominal type check and does +not reliably determine the type. It used to be that a brand check was only possible for built in +types. For a more detailed explanation, see [this write +up](https://github.com/tc39/how-we-work/pull/30#issuecomment-391588889) -Imagine a library that implements dom queries and returns a `query` object. The author of this +It is now possible to implement brand checks in user space as long as there is a way to identify that the object is unique. + +Imagine a library that implements DOM queries and returns a `query` object. The author of this library may be interested in being able to modify the implementation of the `query` object without breaking the programs of users of the library. However, returning plain objects such as ` { type: "queryResult", elements: [ ...... ] }` is not safe, as anyone can return such an object and create a @@ -63,7 +66,7 @@ const queries = new WeakMap(); class Query { // ... - performQuery(queryString { + performQuery(queryString) { // returns the query object return { type: "queryResult", elements: [ ...... ] }; } @@ -74,7 +77,7 @@ class Query { set query(queryString) { // generate a query object - const query = performQuery(queryString); + const query = this.performQuery(queryString); // use the object itself as the key queries.set(query, ...); } From 17ccde88735f6e7aec1c444f23c07d6238a5f6e6 Mon Sep 17 00:00:00 2001 From: codehag Date: Tue, 27 Nov 2018 04:10:06 -0800 Subject: [PATCH 3/5] address comments --- terminology.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/terminology.md b/terminology.md index 26acbe5..c9f6b84 100644 --- a/terminology.md +++ b/terminology.md @@ -4,7 +4,7 @@ These aren’t, by any means, meant to be taken as patterns/principles in that, ### How to add definitions -When you add a definition, make sure that the definition applies to how the TC39 uses it. Some other +When you add a definition, make sure that the definition applies to how TC39 uses it. Some other communities might have similar terms, but they mean a different thing in this case. Otherwise, feel free to reference well known definitions so that people know what they mean. @@ -37,7 +37,7 @@ name should be. We should avoid such bikeshedding. #### Definition: -Brand check ("brand" as in a mark, or a brand made with a branding iron) is a term used by the TC39 +Brand check ("brand" as in a mark, or a brand made with a branding iron) is a term used by TC39 to describe a check against a unique datatype whose creation is controlled by a piece of code. #### Example: @@ -86,7 +86,8 @@ class Query { #### Sources -[esdiscuss comment](https://esdiscuss.org/topic/tostringtag-spoofing-for-null-and-undefined#content-3) +[ES Discuss comment](https://esdiscuss.org/topic/tostringtag-spoofing-for-null-and-undefined#content-3) +- [Clarifying comment on GitHub](https://github.com/tc39/how-we-work/pull/30#issuecomment-391588889) ### Temporal dead zone (TDZ) From aba1ac0423e52797fb839e127a7e185e2c558a00 Mon Sep 17 00:00:00 2001 From: Ross Kirsling Date: Tue, 27 Nov 2018 10:32:24 -0800 Subject: [PATCH 4/5] Update terminology.md Co-Authored-By: codehag --- terminology.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/terminology.md b/terminology.md index 8e11517..0967585 100644 --- a/terminology.md +++ b/terminology.md @@ -44,7 +44,7 @@ to describe a check against a unique datatype whose creation is controlled by a One example of this is built in JavaScript datatypes, which are unique and cannot be made in user space. `Array.isArray` is an example of a brand check. For reference see [this -discussion](https://esdiscuss.org/topic/tostringtag-spoofing-for-null-and-undefined#content-3) +discussion](https://esdiscuss.org/topic/tostringtag-spoofing-for-null-and-undefined#content-3). A common misconception is that `instanceof` is a brand check. This is a nominal type check and does not reliably determine the type. It used to be that a brand check was only possible for built in From 8661b0f322d5fc6beee312baeff5dcfdbcfb6d31 Mon Sep 17 00:00:00 2001 From: Ross Kirsling Date: Tue, 27 Nov 2018 12:38:00 -0800 Subject: [PATCH 5/5] Update terminology.md Co-Authored-By: codehag --- terminology.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/terminology.md b/terminology.md index 0967585..2c0b2fe 100644 --- a/terminology.md +++ b/terminology.md @@ -49,7 +49,7 @@ discussion](https://esdiscuss.org/topic/tostringtag-spoofing-for-null-and-undefi A common misconception is that `instanceof` is a brand check. This is a nominal type check and does not reliably determine the type. It used to be that a brand check was only possible for built in types. For a more detailed explanation, see [this write -up](https://github.com/tc39/how-we-work/pull/30#issuecomment-391588889) +up](https://github.com/tc39/how-we-work/pull/30#issuecomment-391588889). It is now possible to implement brand checks in user space as long as there is a way to identify that the object is unique.