Skip to content

Commit

Permalink
Move symbols to be on Symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Oct 20, 2024
1 parent 00f2561 commit b7262f6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
12 changes: 7 additions & 5 deletions src/internal/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface MemberInit {
length?: number;
}

/** @deprecated */
export const init: typeof Symbol.struct_init = Symbol.struct_init;

/**
Expand All @@ -35,11 +36,12 @@ export interface Metadata {
size: number;
}

/** @deprecated */
export const metadata: typeof Symbol.struct_metadata = Symbol.struct_metadata;

export interface _DecoratorMetadata<T extends Metadata = Metadata> extends DecoratorMetadata {
[metadata]?: T;
[init]?: MemberInit[];
[Symbol.struct_metadata]?: T;
[Symbol.struct_init]?: MemberInit[];
}

export interface DecoratorContext<T extends Metadata = Metadata> {
Expand All @@ -50,7 +52,7 @@ export type MemberContext = ClassMemberDecoratorContext & DecoratorContext;

export interface Static<T extends Metadata = Metadata> {
[Symbol.metadata]: DecoratorMetadata & {
[metadata]: T;
[Symbol.struct_metadata]: T;
};
new (): Instance<T>;
prototype: Instance<T>;
Expand All @@ -63,9 +65,9 @@ export interface StaticLike<T extends Metadata = Metadata> extends ClassLike {
export function isValidMetadata<T extends Metadata = Metadata>(
arg: unknown
): arg is DecoratorMetadata & {
[metadata]: T;
[Symbol.struct_metadata]: T;
} {
return arg != null && typeof arg == 'object' && metadata in arg;
return arg != null && typeof arg == 'object' && Symbol.struct_metadata in arg;
}

/**
Expand Down
18 changes: 9 additions & 9 deletions src/struct.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as primitive from './internal/primitives.js';
import type { DecoratorContext, InstanceLike, Member, MemberInit, Metadata, Options, Size, StaticLike } from './internal/struct.js';
import { checkInstance, checkStruct, init, isStatic, metadata, symbol_metadata, type MemberContext } from './internal/struct.js';
import { checkInstance, checkStruct, isStatic, symbol_metadata, type MemberContext } from './internal/struct.js';
import { capitalize } from './string.js';
import type { ClassLike } from './types.js';
export * as Struct from './internal/struct.js';
Expand All @@ -20,7 +20,7 @@ export function sizeof<T extends primitive.Valid | StaticLike | InstanceLike>(ty

const struct = isStatic(type) ? type : type.constructor;

return struct[symbol_metadata(struct)][metadata].size as Size<T>;
return struct[symbol_metadata(struct)][Symbol.struct_metadata].size as Size<T>;
}

/**
Expand All @@ -36,10 +36,10 @@ export function align(value: number, alignment: number): number {
export function struct(options: Partial<Options> = {}) {
return function _decorateStruct<const T extends StaticLike>(target: T, context: ClassDecoratorContext & DecoratorContext): T {
context.metadata ??= {};
context.metadata[init] ||= [];
context.metadata[Symbol.struct_init] ||= [];
let size = 0;
const members = new Map<string, Member>();
for (const _ of context.metadata[init]) {
for (const _ of context.metadata[Symbol.struct_init]!) {
const { name, type, length } = _;
if (!primitive.isValid(type) && !isStatic(type)) {
throw new TypeError('Not a valid type: ' + type);
Expand All @@ -53,7 +53,7 @@ export function struct(options: Partial<Options> = {}) {
size = align(size, options.align || 1);
}

context.metadata[metadata] = { options, members, size } satisfies Metadata;
context.metadata[Symbol.struct_metadata] = { options, members, size } satisfies Metadata;
return target;
};
}
Expand All @@ -74,8 +74,8 @@ export function member(type: primitive.Valid | ClassLike, length?: number) {
}

context.metadata ??= {};
context.metadata[init] ||= [];
context.metadata[init].push({ name, type, length } satisfies MemberInit);
context.metadata[Symbol.struct_init] ||= [];
context.metadata[Symbol.struct_init]!.push({ name, type, length } satisfies MemberInit);
return value;
};
}
Expand All @@ -85,7 +85,7 @@ export function member(type: primitive.Valid | ClassLike, length?: number) {
*/
export function serialize(instance: unknown): Uint8Array {
checkInstance(instance);
const { options, members } = instance.constructor[symbol_metadata(instance.constructor)][metadata];
const { options, members } = instance.constructor[symbol_metadata(instance.constructor)][Symbol.struct_metadata];

const buffer = new Uint8Array(sizeof(instance));
const view = new DataView(buffer.buffer);
Expand Down Expand Up @@ -129,7 +129,7 @@ export function serialize(instance: unknown): Uint8Array {
*/
export function deserialize(instance: unknown, _buffer: ArrayBuffer | ArrayBufferView) {
checkInstance(instance);
const { options, members } = instance.constructor[symbol_metadata(instance.constructor)][metadata];
const { options, members } = instance.constructor[symbol_metadata(instance.constructor)][Symbol.struct_metadata];

const buffer = _buffer instanceof Uint8Array ? _buffer : new Uint8Array('buffer' in _buffer ? _buffer.buffer : _buffer);

Expand Down

0 comments on commit b7262f6

Please sign in to comment.