generated from tc39/template-for-proposals
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbinary.js
48 lines (43 loc) · 1.44 KB
/
binary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import {Call, ToObject, IsCallable} from './common/abstract.js'
import {ThrowTypeError, IsClassConstructor, IsBoundFunction, IsArrowFunction} from './common/extra.js'
// x::method(...args) -> CallExtMethod($method, x, args)
export function CallExtMethod(extensionMethod, thisArgument, argumentsList) {
return Call(extensionMethod.invoke, thisArgument, argumentsList)
}
// x::prop -> CallExtGetter($prop, x)
export function CallExtGetter(extensionAccessor, thisArgument) {
return Call(extensionAccessor.get, thisArgument, [])
}
// x::prop = value -> CallExtSetter($prop, x, value)
export function CallExtSetter(extensionAccessor, thisArgument, V) {
Call(extensionAccessor.set, thisArgument, [V])
return V
}
// const ::method = o -> $method = CreateExtMethod(o)
export function CreateExtMethod(O) {
O = ToObject(O)
if (IsCallable(O)) {
CheckMethod(O)
return {invoke: O}
}
const {get, set, value} = O
if (get !== undefined || set !== undefined) {
if (get !== undefined) CheckMethod(get)
if (set !== undefined) CheckMethod(set)
return {
get, set,
invoke(...args) {
const f = Call(get, this)
return Call(f, this, args)
},
}
}
CheckMethod(value)
return {invoke: value}
}
function CheckMethod(F) {
if (!IsCallable(F)) ThrowTypeError()
if (IsArrowFunction(F) || IsBoundFunction(F)) ThrowTypeError()
if (IsClassConstructor(F)) ThrowTypeError()
// Optional: throw type error for other non-methods (functions ignore receiver)
}