generated from tc39/template-for-proposals
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathExtension.js
133 lines (117 loc) · 3.92 KB
/
Extension.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { Call, ToObject, IsCallable, DefinePropertyOrThrow, ToLength } from './common/abstract.js'
import { ObjectCreate, OwnPropertyStringKeys, ThrowTypeError, IsClassConstructor } from './common/extra.js'
import InternalSlot from './common/InternalSlot.js'
import { SymbolExtension } from './ternary.js'
import { CreateExtMethod, CallExtMethod, CallExtGetter, CallExtSetter } from './binary.js'
const ExtensionMethods = InternalSlot()
function ExtensionGetMethod(extension, name) {
const methods = ExtensionMethods.get(extension)
const m = methods[name]
if (m === undefined) ThrowTypeError()
return m
}
export default class Extension {
constructor(extensionMethods) {
extensionMethods = ToObject(extensionMethods)
const methods = ObjectCreate(null)
for (const name of OwnPropertyStringKeys(extensionMethods)) {
methods[name] = CreateExtMethod(extensionMethods[name])
}
ExtensionMethods.install(this, methods)
}
get [SymbolExtension]() { return this }
invoke(receiver, name, args, O) {
const m = ExtensionGetMethod(this, name)
return CallExtMethod(m, receiver, args)
}
get(receiver, name, O) {
const m = ExtensionGetMethod(this, name)
return CallExtGetter(m, receiver)
}
set(receiver, name, V, O) {
const m = ExtensionGetMethod(this, name)
return CallExtSetter(m, receiver, V)
}
static from(source) {
const ext = source[SymbolExtension]
if (ext !== undefined) return ToObject(ext)
if (IsConstructor(source)) return new Extension(CollectMethods(source.prototype, true), option)
// for first-class protocol proposal
// if (IsProtocol(source)) return new Extension(CreateMethodsFromProtocol(source), option)
return new Extension(CollectMethods(source, false), option)
}
static method(value, options = undefined) {
return ToMethod('invoke', value, options)
}
static accessor(get, set, arg3 = undefined, arg4 = undefined) {
let getOptions, setOptions
if (IsCallable(set)) {
setOptions = arg3
} else {
getOptions = set
set = arg3
setOptions = arg4
}
const result = {}
if (get !== undefined && get !== null) result.get = ToMethod('get', get, getOptions)
if (set !== undefined && set !== null) result.set = ToMethod('set', set, setOptions)
return result
}
}
function CollectMethods(O, isProto) {
const names = OwnPropertyStringKeys(O)
for (const k of names) {
const v = isProto
? ExtMethodFromPropDesc(GetOwnPropertyDescriptor(O, k))
: ExtMethodFromMethod(O, k)
if (v !== undefined) extension[k] = v
}
return extension
}
function ExtMethodFromPropDesc(pd) {
if (pd === undefined) return undefined
const {value, get, set} = pd
if (get || set) return {get, set}
if (IsCallable(value)) return value
return undefined
}
function ExtMethodFromMethod(o, k) {
const v = o[k]
if (IsCallable(v) && !IsClassConstructor(v)) {
return ToMethod('invoke', BindThisArg(v, o))
}
return undefined
}
function ToMethod(type, V, options) {
if (!IsCallable(V)) throw new TypeError()
if (IsClassConstructor(V)) throw new TypeError()
options ??= {}
const {receiver} = options
let first = false, last = false, spread = false
if (receiver) {
for (const token of String(receiver).trim().split(/\s+/)) {
switch (token) {
case 'spread': spread = true; break
case 'last': last = true; break
case 'first': first = true; break
default: throw new TypeError()
}
}
if (first && last) throw new TypeError()
}
const f = spread
? last
? function (...args) { return V(...args, ...this) }
: function (...args) { return V(...this, ...args) }
: last
? function (...args) { return V(...args, this) }
: function (...args) { return V(this, ...args) }
DefinePropertyOrThrow(f, 'name', {value: `${type} ${V.name}`})
DefinePropertyOrThrow(f, 'length', {value: ToLength(V.length - 1)})
return f
}
export function BindThisArg(F, O) {
if (IsBoundFunction(F) || IsArrowFunction(F)) return F
return Call(FunctionPrototypeBind, F, [O])
}
const FunctionPrototypeBind = Function.prototype.bind