-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates to function and namespace registring
- Loading branch information
Showing
17 changed files
with
618 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { StatefulZirconValidator } from "./StatefulZirconValidator"; | ||
import { InferTypeFromValidator2, ZirconValidator } from "./ZirconTypeValidator"; | ||
|
||
export class OptionalValidator<T, U = T> extends StatefulZirconValidator<T | undefined, U | undefined> { | ||
public constructor(private innerValidator: ZirconValidator<T, U>) { | ||
super(innerValidator.Type + "?"); | ||
} | ||
|
||
public Validate(value: unknown): value is T | undefined { | ||
return this.innerValidator.Validate(value) || value === undefined; | ||
} | ||
|
||
public Transform(value: T): U | undefined { | ||
return this.innerValidator.Transform?.(value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { ZirconValidator } from "./ZirconTypeValidator"; | ||
|
||
export abstract class StatefulZirconValidator<T, U = never> implements ZirconValidator<T, U> { | ||
public constructor(public Type: string) {} | ||
public abstract Validate(value: unknown): value is T; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import ZrContext from "@rbxts/zirconium/out/Data/Context"; | ||
import ZrLuauFunction, { ZrLuauArgument } from "@rbxts/zirconium/out/Data/LuauFunction"; | ||
import ZrPlayerScriptContext from "@rbxts/zirconium/out/Runtime/PlayerScriptContext"; | ||
import { ZirconFunctionBuilder } from "./ZirconFunctionBuilder"; | ||
import { InferArguments, Validator, ZirconValidator } from "./ZirconTypeValidator"; | ||
|
||
export class ZirconContext { | ||
constructor(private innerContext: ZrContext) {} | ||
public GetExecutor() { | ||
const executor = this.innerContext.getExecutor(); | ||
assert(executor); | ||
return executor; | ||
} | ||
|
||
public GetOutput() { | ||
return this.innerContext.getOutput(); | ||
} | ||
|
||
public GetInput() { | ||
return this.innerContext.getInput(); | ||
} | ||
} | ||
|
||
export class ZirconFunction<V extends readonly ZirconValidator<unknown, unknown>[], R> extends ZrLuauFunction { | ||
public constructor( | ||
private name: string, | ||
private argumentValidators: V, | ||
private zirconCallback: (context: ZirconContext, ...args: InferArguments<V>) => R, | ||
) { | ||
super((context, ...args) => { | ||
// We'll need to type check all the arguments to ensure they're valid | ||
// and transform as appropriate for the user side | ||
|
||
let transformedArguments = new Array<defined>(); | ||
if (this.argumentValidators.size() > 0) { | ||
for (let i = 0; i < this.argumentValidators.size(); i++) { | ||
const validator = this.argumentValidators[i]; | ||
const argument = args[i]; | ||
if (validator && validator.Validate(argument)) { | ||
if (validator.Transform !== undefined) { | ||
print("validate", i); | ||
transformedArguments[i] = validator.Transform(argument) as defined; | ||
} else { | ||
print("noValidate", i); | ||
transformedArguments[i] = argument; | ||
} | ||
} else { | ||
// TODO: Error message based on type, argument index etc. | ||
error("Failed at argument" + i); | ||
} | ||
} | ||
} else { | ||
transformedArguments = args as Array<ZrLuauArgument>; | ||
} | ||
|
||
/// This is not pretty, I know. | ||
this.zirconCallback( | ||
new ZirconContext(context), | ||
...((transformedArguments as unknown) as InferArguments<V>), | ||
); | ||
}); | ||
} | ||
|
||
public GetName() { | ||
return this.name; | ||
} | ||
|
||
private GetArgumentTypes() { | ||
return this.argumentValidators.map((v) => v.Type); | ||
} | ||
|
||
/** @internal */ | ||
public RegisterToContext(context: ZrPlayerScriptContext) { | ||
context.registerGlobal(this.name, this); | ||
} | ||
|
||
public toString() { | ||
return ( | ||
`function ${this.name}(` + | ||
this.GetArgumentTypes() | ||
.map((typeName, argIndex) => `a${argIndex}: ${typeName}`) | ||
.join(", ") + | ||
") { [ZirconFunction] }" | ||
); | ||
} | ||
|
||
public static args<V extends readonly Validator[]>(...value: V) { | ||
return value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { | ||
BuiltInValidators, | ||
InferArguments, | ||
InferValidator, | ||
InferValidators, | ||
Validator, | ||
ZirconArgument, | ||
ZirconValidator, | ||
} from "./ZirconTypeValidator"; | ||
import { ZirconContext, ZirconFunction } from "./ZirconFunction"; | ||
|
||
export class ZirconFunctionBuilder<V extends ZirconValidator<any, any>[] = []> { | ||
private validators = new Array<ZirconValidator<any, any>>(); | ||
private hasVaradic = false; | ||
public constructor(private name: string) {} | ||
|
||
/** | ||
* Adds these arguments to this function | ||
* @param args The arguments | ||
* @returns | ||
*/ | ||
public AddArguments<TValidation extends Validator[]>(...args: TValidation) { | ||
if (this.hasVaradic) { | ||
throw `Cannot add argument past varadic argument`; | ||
} | ||
|
||
for (const argValidator of args) { | ||
if (typeIs(argValidator, "string")) { | ||
this.validators.push(BuiltInValidators[argValidator]); | ||
} else { | ||
this.validators.push(argValidator); | ||
} | ||
} | ||
return (this as unknown) as ZirconFunctionBuilder<[...V, ...InferValidators<TValidation>]>; | ||
} | ||
|
||
public AddArgument<TValidation extends Validator>(type: TValidation) { | ||
return (this as unknown) as ZirconFunctionBuilder<[...V, ...InferValidator<TValidation>[]]>; | ||
} | ||
|
||
public AddOptionalArgument<TValidation extends Validator>(type: TValidation) {} | ||
|
||
/** @internal */ | ||
public AddVaradicArgument<TValidation extends Validator>(arg: TValidation) { | ||
this.hasVaradic = true; | ||
|
||
return (this as unknown) as Omit< | ||
ZirconFunctionBuilder<[...V, ...InferValidator<TValidation>[]]>, | ||
"AddArguments" | "AddVaradicArgument" | ||
>; | ||
} | ||
|
||
public Bind(fn: (context: ZirconContext, ...args: InferArguments<V>) => void) { | ||
return new ZirconFunction(this.name, this.validators as V, fn); | ||
} | ||
} | ||
|
||
new ZirconFunctionBuilder("test").AddArguments("player", "object", "unknown").Bind((context, pl, obj, val) => {}); |
Oops, something went wrong.