format
This commit is contained in:
45
src/index.js
45
src/index.js
@ -1,13 +1,22 @@
|
||||
import { limits } from "./limits"
|
||||
import { memcpy } from "./mem"
|
||||
import { Type } from "./Type"
|
||||
import { Int } from "./Int"
|
||||
import { limits } from './limits'
|
||||
import { memcpy } from './mem'
|
||||
import { Type } from './Type'
|
||||
import { Int } from './Int'
|
||||
import { ConstString } from './ConstString'
|
||||
import { ConstArray } from "./ConstArray"
|
||||
import { ConstArray } from './ConstArray'
|
||||
import { ConstDataView } from './ConstDataView'
|
||||
import { Struct } from './Struct'
|
||||
|
||||
export { limits, memcpy, Type, Int, ConstString, ConstArray, ConstDataView, Struct }
|
||||
export {
|
||||
limits,
|
||||
memcpy,
|
||||
Type,
|
||||
Int,
|
||||
ConstString,
|
||||
ConstArray,
|
||||
ConstDataView,
|
||||
Struct,
|
||||
}
|
||||
|
||||
export function serialize(dv, src, ...types) {
|
||||
const [type, ...inner_types] = types
|
||||
@ -60,7 +69,10 @@ export function serialize(dv, src, ...types) {
|
||||
let offset = 4 + item_head_size * size
|
||||
|
||||
for (let i = 0; i < size; i++) {
|
||||
const item_head_frame = new DataView(dv.buffer, dv.byteOffset + 4 + item_head_size * i)
|
||||
const item_head_frame = new DataView(
|
||||
dv.buffer,
|
||||
dv.byteOffset + 4 + item_head_size * i,
|
||||
)
|
||||
if (item_headless) {
|
||||
item_head_frame.setUint32(0, offset)
|
||||
const item_frame = new DataView(dv.buffer, dv.byteOffset + offset)
|
||||
@ -114,7 +126,10 @@ export function parse(dv, ...types) {
|
||||
const array = Array(size)
|
||||
|
||||
for (let i = 0; i < size; i++) {
|
||||
const item_head_frame = new DataView(dv.buffer, dv.byteOffset + 4 + item_head_size * i)
|
||||
const item_head_frame = new DataView(
|
||||
dv.buffer,
|
||||
dv.byteOffset + 4 + item_head_size * i,
|
||||
)
|
||||
if (item_headless) {
|
||||
const offset = item_head_frame.getUint32(0)
|
||||
const item_frame = new DataView(dv.buffer, dv.byteOffset + offset)
|
||||
@ -146,12 +161,14 @@ export function isHeadless(...args) {
|
||||
if (arg instanceof Type) {
|
||||
return arg.isHeadless(...inner_args)
|
||||
}
|
||||
return arg == Array ||
|
||||
return (
|
||||
arg == Array ||
|
||||
arg == String ||
|
||||
arg == DataView ||
|
||||
Array.isArray(arg) ||
|
||||
typeof arg == 'string' ||
|
||||
arg instanceof DataView
|
||||
)
|
||||
}
|
||||
|
||||
export function sizeofHead(...args) {
|
||||
@ -166,10 +183,10 @@ export function sizeof(...args) {
|
||||
const [arg, ...inner_args] = args
|
||||
const [arg2, ...inner_args2] = inner_args
|
||||
|
||||
if (arg == Boolean || arg2 == Boolean && typeof arg == 'boolean') {
|
||||
if (arg == Boolean || (arg2 == Boolean && typeof arg == 'boolean')) {
|
||||
return 1
|
||||
}
|
||||
if (arg == Number || arg2 == Number && typeof arg == 'number') {
|
||||
if (arg == Number || (arg2 == Number && typeof arg == 'number')) {
|
||||
return 8
|
||||
}
|
||||
if (arg2 == String && typeof arg == 'string') {
|
||||
@ -177,10 +194,8 @@ export function sizeof(...args) {
|
||||
return 4 + encoder.encode(arg).byteLength
|
||||
}
|
||||
if (arg2 == Array && Array.isArray(arg)) {
|
||||
|
||||
const fixed_size = 4 + sizeofHead(...inner_args2) * arg.length
|
||||
if (isHeadless(...inner_args2)) {
|
||||
|
||||
let variable_size = 0
|
||||
for (const item of arg) {
|
||||
variable_size += sizeof(item, ...inner_args2)
|
||||
@ -204,10 +219,12 @@ export function sizeof(...args) {
|
||||
}
|
||||
|
||||
export function isSerializableType(type) {
|
||||
return type == Boolean ||
|
||||
return (
|
||||
type == Boolean ||
|
||||
type == Number ||
|
||||
type == String ||
|
||||
type == Array ||
|
||||
type == DataView ||
|
||||
type instanceof Type
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user