create: (Arrays, Interop, fail, isGroup, isOutTarget, isParameterDeclaration, isReturnDeclaration, make, metadata, nativeType, operation, owners, pointerType, providerContext, register, requireArity, requireString, voidDescriptor, getNativeContext) => {
opaque: null
nullable: null
owned: null
out: null
publish: null
declareBinding: null
bind: null
bindPreserving: null
releaseOwned: null
release: null
addressOf: null
methods: {
opaque: (library, nativeTypeName, ...extra) => {
(extra.size() == 0).ifFalse(() => fail())
requireString(nativeTypeName)
native: operation(library, "opaque", nativeTypeName)
make("opaque", library, native, null)
}
nullable: (opaqueDescriptor, ...extra) => {
(extra.size() == 0).ifFalse(() => fail())
record: metadata(opaqueDescriptor)
result: null
if(record.layoutKind == "pointer") {
result = register("nullablePointer", "nullablePointer", record.size, record.align, null, null,
opaqueDescriptor, record.parts)
} else {
(record.kind == "opaque").ifFalse(() => fail())
native: operation(record.library, "nullable", record.native)
result = make("nullable", record.library, native, opaqueDescriptor)
}
result
}
owned: (opaqueDescriptor, releaseSymbol, releaseReturnDescriptor, ...extra) => {
(extra.size() == 0).ifFalse(() => fail())
record: metadata(opaqueDescriptor)
returns: metadata(releaseReturnDescriptor)
(record.kind == "opaque").ifFalse(() => fail())
((returns.kind == "void") || (returns.kind == "i32")).ifFalse(() => fail())
requireString(releaseSymbol)
native: operation(record.library, "owned", record.native, releaseSymbol,
nativeType(record.library, releaseReturnDescriptor))
make("owned", record.library, native, opaqueDescriptor)
}
out: (descriptor, ...extra) => {
(extra.size() == 0).ifFalse(() => fail())
record: metadata(descriptor)
isOutTarget(record).ifFalse(() => fail())
make("out", null, null, descriptor)
}
publish: (library, descriptor, value) => {
record: metadata(descriptor)
if((record.kind == "owned") && (value !== null)) {
owners[value] = library
}
if(isGroup(record)) {
owners[value] = getNativeContext()
}
value
}
declareBinding: (library, symbol, returnDescriptor, argumentDescriptors, preserving) => {
requireString(symbol)
Array.recognizes(argumentDescriptors).ifFalse(() => fail())
parameters: Array(...argumentDescriptors)
isReturnDeclaration(metadata(returnDescriptor)).ifFalse(() => fail())
positions: 1
for(parameter in parameters) {
record: metadata(parameter)
isParameterDeclaration(record).ifFalse(() => fail())
if(record.kind == "out") { positions = positions + 1 }
}
signature: operation(library, "signature", nativeType(library, returnDescriptor))
staged: Array(returnDescriptor, ...parameters)
position: 1
for(parameter in parameters) {
signature = operation(library, "param", signature, nativeType(library, parameter))
record: metadata(parameter)
if(record.kind == "out") {
staged[position] = record.target
position = position + 1
}
}
outputs: Arrays.take(staged, positions)
if(preserving) {
signature = operation(library, "preserving", signature)
}
parameters.freeze()
outputs.freeze()
binding: operation(library, "bind", symbol, signature)
(...arguments) => {
result: Interop.invoke(binding, ...arguments)
answer: null
if(positions == 1) {
answer = publish(library, returnDescriptor, result)
}
if(positions > 1) {
answer = Array(...outputs)
index: 0
for(descriptor in outputs) {
answer[index] = publish(library, descriptor, result[index])
index = index + 1
}
}
answer
}
}
bind: (library, symbol, returnDescriptor, argumentDescriptors, ...extra) => {
requireArity(extra)
declareBinding(library, symbol, returnDescriptor, argumentDescriptors, false)
}
bindPreserving: (library: fail(), symbol: fail(), returnDescriptor: fail(),
argumentDescriptors: fail(), ...extra) => {
requireArity(extra)
declareBinding(library, symbol, returnDescriptor, argumentDescriptors, true)
}
releaseOwned: (ownedHandle) => {
owners.containsKey(ownedHandle).ifFalse(() => fail())
library: owners.at(ownedHandle)
owners.remove(ownedHandle)
operation(library, "release", ownedHandle)
}
release: (ownedHandle, ...extra) => {
(extra.size() == 0).ifFalse(() => fail())
releaseOwned(ownedHandle)
}
addressOf: (value: fail(), ...extra) => {
requireArity(extra)
result: null
libraryOwned: false
if(owners.containsKey(value)) {
owner: owners.at(value)
if((owner === getNativeContext()) == false) {
result = operation(owner, "addressOf", value, pointerType(owner, voidDescriptor))
libraryOwned = true
}
}
if(libraryOwned == false) {
native: providerContext()
result = operation(native, "regionAddress", value, pointerType(native, voidDescriptor))
}
result
}
}
opaque = methods.opaque
nullable = methods.nullable
owned = methods.owned
out = methods.out
publish = methods.publish
declareBinding = methods.declareBinding
bind = methods.bind
bindPreserving = methods.bindPreserving
releaseOwned = methods.releaseOwned
release = methods.release
addressOf = methods.addressOf
{
opaque: opaque
nullable: nullable
owned: owned
out: out
publish: publish
declareBinding: declareBinding
bind: bind
bindPreserving: bindPreserving
releaseOwned: releaseOwned
release: release
addressOf: addressOf
}
}