Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tperale committed Dec 11, 2024
1 parent 3061928 commit 054f5f2
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { withBinspectorContext } from '../context'
import { BinaryReader, BinaryCursorEndianness } from '../cursor'

function expectReadTest<Target> (buffer: Array<number>, ObjectDefinition: InstantiableObject<Target>, endian: BinaryCursorEndianness = BinaryCursorEndianness.BigEndian, ...args: any[]) {
return expect(binread(new BinaryReader(new Uint8Array(buffer).buffer, endian), ObjectDefinition, ...args))
return expect(binread(new BinaryReader(new Uint8Array(buffer).buffer, endian), ObjectDefinition, {}, ...args))
}

function expectReadTestToThrow<Target> (buffer: Array<number>, ObjectDefinition: InstantiableObject<Target>) {
Expand Down
58 changes: 58 additions & 0 deletions src/decorators/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { PropertyMetaDescriptor } from "./common"

Check failure on line 1 in src/decorators/context.ts

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote

export const CtxSymbol = Symbol('context')

export type GlobalCtx = object

export type CtxKeyFunction<This> = (targetInstance: This) => string | string[]
export type CtxGetterFunction<This> = (targetInstance: This) => any
export type CtxSetterFunction<This> = (targetInstance: This) => any

export interface CtxOptions {
/**
* Ensures that a relation exists before defining the Transformer decorator.
*/
primitiveCheck: boolean
base_type: [] | {}| undefined
}

export const CtxOptionsDefault = {
primitiveCheck: true,
base_type: undefined,
}

/**
* Ctx metadata type definition.
*
* @extends {PropertyMetaDescriptor}
*/
export interface Ctx<This, Value> extends PropertyMetaDescriptor<This> {
options: CtxOptions
keyGetter: CtxKeyFunction<This>
transformer: CtxGetTransformFunction | CtxSetTransformFunction
}

function ctxPropertyFunctionDecoratorFactory () {

}

export function CtxGet<This> (key: ContextKeyFunction | string): ClassAndPropertyDecoratorType<This> {
}

export function CtxSetArray<This> (key: string): ClassAndPropertyDecoratorType<This> {
}

export function CtxSet<This> (key: string): ClassAndPropertyDecoratorType<This> {
return function (_: any, context: ClassAndPropertyDecoratorContext<This>) {
ctxPropertyFunctionDecoratorFactory<This> ('ctx-set', PreFunctionSymbol, (targetInstance) => {
}, opt)(_, context)
}
}

export function useContext<This> (ctx: Array<Ctx<This>>, targetInstance: This, ctx: object): void {
ctx.forEach((x) => {
if ((x.options.scope & scope) > 0) {
x.func(targetInstance, cursor)
}
})
}
19 changes: 19 additions & 0 deletions src/metadatas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { type Controller, ControllerSymbol } from './decorators/controller'
import { type Transformer, TransformerSymbol } from './decorators/transformer'
import { type Validator, ValidatorSymbol } from './decorators/validator'
import { type BitField, BitFieldSymbol } from './decorators/bitfield'
import { type Ctx, CtxSymbol } from './decorators/context'
import { type PrePost, PreFunctionSymbol, PostFunctionSymbol, PreClassFunctionSymbol, PostClassFunctionSymbol, PrePostSymbols, PrePostClass } from './decorators/prepost'
import { type DecoratorMetadataObject } from './types'

Expand Down Expand Up @@ -252,6 +253,22 @@ function setBitField<This> (
return bitfields
}

function getContext<This, Value> (metadata: DecoratorMetadataObject, propertyKey: keyof This): Array<Validator<This, Value>> {
return getMetadata(
metadata,
propertyKey,
CtxSymbol,
)
}

function setContext<This, Value> (
metadata: DecoratorMetadataObject,
propertyKey: keyof This,
ctx: Ctx<This, Value>,
): Array<Ctx<This, Value>> {
return setMetadata(metadata, propertyKey, ctx.type, ctx)
}

export default {
getMetadata,
setMetadata,
Expand All @@ -276,4 +293,6 @@ export default {
getBitField,
getBitFields,
setBitField,
getContext,
setContext,
}
4 changes: 2 additions & 2 deletions src/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import { useBitField } from './decorators/bitfield'
* `ObjectDefinition` passed in param.
* You can create self refering field by using conditionnal decorator.
*/
export function binread<Target> (content: Cursor, ObjectDefinition: InstantiableObject<Target>, ...args: any[]): Target {
export function binread<Target> (content: Cursor, ObjectDefinition: InstantiableObject<Target>, ctx = {}, ...args: any[]): Target {
const ObjectDefinitionName = ObjectDefinition.name
function getBinReader (field: PropertyType<Target>, instance: Target): ControllerReader {
if (isPrimitiveRelation(field)) {
Expand All @@ -63,7 +63,7 @@ export function binread<Target> (content: Cursor, ObjectDefinition: Instantiable
}

try {
return binread(content, field.relation, ...[...finalArgs, instance])
return binread(content, field.relation, ctx, ...[...finalArgs, instance])
} catch (error) {
// We need to catch the EOF error because the binread function
// can't return it so it just throw it EOFError.
Expand Down

0 comments on commit 054f5f2

Please sign in to comment.