2020-02-09 14:06:45 +00:00
|
|
|
// Nashorn JSAdapter See https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions#Nashornextensions-JSAdapterconstructor
|
2020-01-15 10:38:22 +00:00
|
|
|
let createProxy = eval(`
|
|
|
|
function(handle){ return new JSAdapter(handle) }
|
|
|
|
`)
|
2020-02-01 17:56:45 +00:00
|
|
|
export interface ProxyHandle extends ProxyHandler<any> {
|
2020-01-31 19:37:56 +00:00
|
|
|
// get: (target: any, name: string) => any
|
|
|
|
// set: (target: any, name: string, value: any) => boolean
|
|
|
|
// construct: (target: any, ...args: any[]) => any
|
|
|
|
// has: (target: any, name: string) => boolean
|
|
|
|
// ownKeys: (target: any) => string[]
|
2020-01-15 10:38:22 +00:00
|
|
|
values: (target: any) => any[];
|
2020-01-31 19:37:56 +00:00
|
|
|
// call: (target: any, name: string, ...args: any[]) => any
|
|
|
|
// deleteProperty: (target: any, name: string) => boolean
|
2020-01-15 10:38:22 +00:00
|
|
|
}
|
|
|
|
export class Proxy {
|
2020-01-31 19:37:56 +00:00
|
|
|
static newProxy(target: any, handle: Partial<ProxyHandle>): any {
|
|
|
|
return new Proxy(target, handle)
|
|
|
|
}
|
|
|
|
constructor(target: any, handle: Partial<ProxyHandle>) {
|
2020-01-15 10:38:22 +00:00
|
|
|
return createProxy({
|
2020-01-31 19:37:56 +00:00
|
|
|
__get__: (name: string) => handle.get ? handle.get(target, name, undefined) : target[name],
|
|
|
|
__put__: (name: string, value: any) => handle.set ? handle.set(target, name, value, undefined) : target[name] = value,
|
2020-02-01 17:56:45 +00:00
|
|
|
__call__: (name: string, ...args: any) => handle.apply ? handle.apply(target, name, args) : target[name].apply(target, args),
|
2020-01-31 19:37:56 +00:00
|
|
|
__new__: (...args: any) => handle.construct ? handle.construct(target, args, target) : new target(...args),
|
2020-01-15 10:38:22 +00:00
|
|
|
__getIds__: () => handle.ownKeys ? handle.ownKeys(target) : Object.keys(target),
|
|
|
|
__getValues__: () => handle.values ? handle.values(target) : Object.values(target),
|
|
|
|
__has__: (name: string) => handle.has ? handle.has(target, name) : Object.getOwnPropertyDescriptor(target, name) != undefined,
|
2020-01-31 19:37:56 +00:00
|
|
|
__delete__: (name: string) => handle.deleteProperty ? handle.deleteProperty(target, name) : delete target[name]
|
2020-01-15 10:38:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|