0

I'm interesting in how JS source code works and seems that I should to use ECMAScript® Language Specification. In my case I tried to write my own implemetetion of Function.prototype.call

Function.prototype._call = function (thisArg = {}, ...arg) {
  // 1. Let func be the this value.
  let func = this
  // 2. If IsCallable(func) is false, throw a TypeError exception.
  if (typeof func !== 'function') {
    throw new TypeError(this + "isn't callable")
  }
  // 3. Perform PrepareForTailCall().
  thisArg.func = func
  // 4. Return ? Call(func, thisArg, args).
  return thisArg.func(...arg)
}

It works, but I'm not sure that my implementation is correct and would like to compare it with the "official" implementation. Where may I find code references for ECMAScript examples? Thank you!

soldy
  • 356
  • 1
  • 11
  • Questions seeking off-site resources are specifically off topic here. That said, both Firefox and Chrome are open source, so you could look at their source code. – Tangentially Perpendicular Jun 12 '22 at 06:43
  • 2
    I don’t see how `thisArg.func = func` achieves PrepareForTailCall(). – Ry- Jun 12 '22 at 06:48
  • Are you earnestly trying to reimplement JavaScript... in JavaScript? – Dai Jun 12 '22 at 06:55
  • 3
    Actually the `_call` as written has a side effect - making the function being called a property of the `this` value supplied as an argument to `_call`. - which the real `call` method doesn't have. "Abstract operations" can sometimes be implemented in JavaScript and sometimes are only pseudo code that has to be implemented for real in a JavaScript engine. – traktor Jun 12 '22 at 07:00
  • 1
    Also, by the way, this will throw if `thisArg` is `undefined`. – CherryDT Jun 12 '22 at 07:01
  • 1
    Related, if not duplicate: [Does a real ECMAScript implementation exist, or is it just a spec?](https://stackoverflow.com/q/1507848/1048572) – Bergi Jun 12 '22 at 07:14

2 Answers2

1

Is there an official implementation of ECMAScript Language Specification?

Such an implementation is known as a reference implementation, rather than an "official" implementation, as surely every conforming and certified (is that even a thing for ECMAScript?) implementation is "official".

But yes, there is - but as far as I can tell, only ECMAScript 4 from 2007 has (had?) an actual reference implementation which was available to download from the ECMAScript.org website.


https://lambda-the-ultimate.org/node/2289

ECMAScript Edition 4 Reference Implementation

The first pre-release of the reference implementation of ECMAScript Edition 4 (a.k.a. JavaScript 2) is now available. We've created a new web site for information about the ECMAScript specification and reference implementation. You can download source and binary forms of the reference implementation.


Dai
  • 141,631
  • 28
  • 261
  • 374
1

No, there is no reference implementation for ECMAScript, the definitive authoriative source is the specification itself - see also its Conformance section. It is accompanied by the official Test262: ECMAScript Test Suite

The closest to what you are looking for would be the Engine 262 project.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375