Skip to content

Commit

Permalink
refactor: use Uint32Array in type tag
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi committed Dec 19, 2023
1 parent 2da3e2b commit d2c6a3c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
25 changes: 12 additions & 13 deletions packages/emnapi/src/wrap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { emnapiCtx } from 'emnapi:shared'
import { wasmMemory } from 'emnapi:emscripten-runtime'
import { emnapiString } from './string'
import { emnapiCreateFunction, emnapiDefineProperty, emnapiWrap, emnapiUnwrap, emnapiGetHandle } from './internal'
import { $CHECK_ARG, $CHECK_ENV, $CHECK_ENV_NOT_IN_GC, $PREAMBLE } from './macro'
Expand Down Expand Up @@ -122,12 +123,9 @@ export function napi_type_tag_object (env: napi_env, object: napi_value, type_ta
if (binding.tag !== null) {
return envObject.setLastError(envObject.tryCatch.hasCaught() ? napi_status.napi_pending_exception : napi_status.napi_invalid_arg)
}
binding.tag = [
$makeGetValue('type_tag', '0', 'u32') as number,
$makeGetValue('type_tag', '4', 'u32') as number,
$makeGetValue('type_tag', '8', 'u32') as number,
$makeGetValue('type_tag', '12', 'u32') as number
]
const tag = new Uint8Array(16)
tag.set(new Uint8Array(wasmMemory.buffer, type_tag, 16))
binding.tag = new Uint32Array(tag.buffer)

return envObject.getReturnStatus()
})
Expand Down Expand Up @@ -157,13 +155,14 @@ export function napi_check_object_type_tag (env: napi_env, object: napi_value, t
const binding = envObject.getObjectBinding(value.value)
if (binding.tag !== null) {
$from64('type_tag')
for (i = 0; i < 4; i++) {
const x = $makeGetValue('type_tag', 'i * 4', 'u32')
if (x !== binding.tag[i]) {
ret = false
break
}
}
const tag = binding.tag
const typeTag = new Uint32Array(wasmMemory.buffer, type_tag, 4)
ret = (
tag[0] === typeTag[0] &&
tag[1] === typeTag[1] &&
tag[2] === typeTag[2] &&
tag[3] === typeTag[3]
)
} else {
ret = false
}
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function handleThrow (envObject: Env, value: any): void {

export interface IReferenceBinding {
wrapped: number // wrapped Reference id
tag: [number, number, number, number] | null
tag: Uint32Array | null
data: void_p
}

Expand Down

0 comments on commit d2c6a3c

Please sign in to comment.