2
0
mirror of https://github.com/esiur/esiur-js.git synced 2026-04-03 21:48:21 +00:00

Generator

This commit is contained in:
2022-09-03 03:08:57 +03:00
parent f08df2c3fa
commit 93ed4edcbd
26 changed files with 1377 additions and 238 deletions

View File

@@ -29,6 +29,7 @@
import AsyncBag from '../Core/AsyncBag.js';
import AsyncReply from '../Core/AsyncReply.js';
import IDestructible from '../Core/IDestructible.js';
import { TemplateDescriber } from './Template/TemplateDescriber.js';
export const ResourceTrigger =
{
@@ -59,11 +60,6 @@ export default class IResource extends IDestructible
static get template()
{
return {
namespace: "Esiur",
properties: [],
functions: [],
events: []
}
return new TemplateDescriber("Esiur", []);
}
}

View File

@@ -0,0 +1,129 @@
import Void from "../../Data/Void.js";
export class TemplateDescriber {
properties;
events;
functions;
constants;
namespace;
version;
parent;
annotation;
classId;
className;
constructor(namespace, members, parent, version = 0, annotation = null, classId = null, className = null){
if (namespace == null)
throw new Error("Namespace name can't be null.");
if (members == null)
throw new Error("Members name can't be null.");
this.namespace = namespace;
this.parent = parent;
this.properties = members.filter(x=>x instanceof Prop);;
this.functions = members.filter(x=>x instanceof Func);
this.events = members.filter(x=>x instanceof Evt);;
this.constants = members.filter(x=>x instanceof Const);;
this.version = version;
this.annotation = annotation;
this.classId = classId;
this.className = className;
}
}
export class Prop {
name;
type;
readAnnotation;
writeAnnotation;
recordable;
constructor(name, type = Object, readAnnotation = null, writeAnnotation = null, recordable = false)
{
if (name == null)
throw new Error("Property name can't be null.");
this.name = name;
this.type = type ?? Object;
this.readAnnotation = readAnnotation;
this.writeAnnotation = writeAnnotation;
this.recordable = recordable;
}
}
export class Evt {
name;
listenable;
type;
annotation;
constructor(name, type = Object, listenable = false, annotation = null)
{
if (name == null)
throw new Error("Event name can't be null.");
this.name = name;
this.type = type ?? Object;
this.listenable = listenable;
this.annotation = annotation;
}
}
export class Const {
name;
type;
annotation;
value;
constructor(name, type = String, value, annotation)
{
if (name == null)
throw new Error("Constant name can't be null.");
this.name = name;
this.type = type ?? String;
this.value = value ?? "";
this.annotation = annotation;
}
}
export class Func {
name;
returnType;
args;
annotation;
isStatic;
constructor(name, returnType = Void, args = [], annotation = null, isStatic = false)
{
if (name == null)
throw new Error("Function name can't be null.");
this.name = name;
this.returnType = returnType ?? Void;
this.args = args ?? [];
this.annotation = annotation;
this.isStatic = isStatic;
}
}
export class Arg {
name;
type;
optional;
constructor(name, type = Object, optional = false)
{
if (name == null)
throw new Error("Argument name can't be null.");
this.name = name;
this.type = type ?? Object;
this.optional = optional;
}
}

View File

@@ -255,74 +255,73 @@ export default class TypeTemplate {
this.definedType = type;
var template = type.template;
let describer = type.template;
// set guid
this.className = template.namespace + "." + type.prototype.constructor.name;
this.classId = SHA256.compute(DC.stringToBytes(this.className)).getGuid(0);
this.className = describer.namespace + "." + (describer.className ?? type.prototype.constructor.name);
this.classId = describer.classId ?? SHA256.compute(DC.stringToBytes(this.className)).getGuid(0);
if (addToWarehouse)
addToWarehouse.putTemplate(this);
Warehouse.putTemplate(this);
//byte currentIndex = 0;
if (template.properties != null)
for (let i = 0; i < template.properties.length; i++) {
if (describer.properties != null)
for (let i = 0; i < describer.properties.length; i++) {
//[name, type, {read: comment, write: comment, recordable: }]
let pi = template.properties[i];
let pt = new PropertyTemplate(this, i, pi[0], false,
RepresentationType.fromType(pi[1]) ?? RepresentationType.Void,
pi[2]?.read, pi[2]?.write, pi[2]?.recordable);
let pi = describer.properties[i];
let pt = new PropertyTemplate(this, i, pi.name, false,
RepresentationType.fromType(pi.type) ?? RepresentationType.Void,
pi.readAnnotation, pi.writeAnnotation, pi.recordable);
pt.propertyInfo = pi;
this.properties.push(pt);
}
if (template.constants != null)
for (let i = 0; i < template.constants.length; i++) {
let ci = template.constants[i];
let ct = new ConstantTemplate(this, i, ci[0], false,
RepresentationType.fromType(ci[1]) ?? RepresentationType.Void,
ci.value, ci.help);
if (describer.constants != null)
for (let i = 0; i < describer.constants.length; i++) {
let ci = describer.constants[i];
let ct = new ConstantTemplate(this, i, ci.name, false,
RepresentationType.fromType(ci.type) ?? RepresentationType.Void,
ci.value, ci.annotation);
ct.propertyInfo = ci;
this.constants.push(ct);
}
if (this.templateType == TemplateType.Resource)
{
if (template.events != null)
if (describer.events != null)
{
for (let i = 0; i < template.events.length; i++) {
for (let i = 0; i < describer.events.length; i++) {
// [name, type, {listenable: true/false, help: ""}]
var ei = template.events[i];
var et = new EventTemplate(this, i, ei[0], false,
RepresentationType.fromType(ei[1]) ?? RepresentationType.Void,
ei[2]?.help, ei[2]?.listenable)
var ei = describer.events[i];
var et = new EventTemplate(this, i, ei.name, false,
RepresentationType.fromType(ei.type) ?? RepresentationType.Void,
ei.annotation, ei.listenable)
et.eventInfo = ei;
this.events.push(et);
}
}
if (template.functions != null)
if (describer.functions != null)
{
for (let i = 0; i < template.functions.length; i++) {
for (let i = 0; i < describer.functions.length; i++) {
var fi = template.functions[i];
var fi = describer.functions[i];
let args = [];
for(let ai = 0; ai < fi[1].length; ai++)
args.push(new ArgumentTemplate(fi[1][ai][0], RepresentationType.fromType(fi[1][ai][1])
?? RepresentationType.Dynamic, fi[1][ai][2]?.optional, ai));
for(let ai = 0; ai < fi.args.length; ai++)
args.push(new ArgumentTemplate(fi.args[ai].name, RepresentationType.fromType(fi.args[ai].type)
?? RepresentationType.Dynamic, fi.args[ai].optional, ai));
// [name, {param1: type, param2: int}, returnType, "Description"]
let isStatic = type[fi[0]] instanceof Function;
let isStatic = type[fi.name] instanceof Function;
var ft = new FunctionTemplate(this, i, fi[0], false, isStatic, args,
RepresentationType.fromType(fi[2]) ?? RepresentationType.Void,
fi[3]);
var ft = new FunctionTemplate(this, i, fi.name, false, isStatic, args,
RepresentationType.fromType(fi.returnType) ?? RepresentationType.Void,
fi.annotation);
ft.methodInfo = fi;
@@ -347,7 +346,7 @@ export default class TypeTemplate {
// bake it binarily
let b = BL();
let hasClassAnnotation = template.annotation != null;
let hasClassAnnotation = describer.annotation != null;
var cls = DC.stringToBytes(this.className);
b.addUint8( (hasClassAnnotation ? 0x40 : 0 ) | this.templateType)
@@ -357,13 +356,13 @@ export default class TypeTemplate {
if (hasClassAnnotation)
{
var classAnnotationBytes = DC.stringToBytes(template.annotation);
var classAnnotationBytes = DC.stringToBytes(describer.annotation);
b.addUint16(classAnnotationBytes.length)
.addUint8Array(classAnnotationBytes);
this.annotation = template.annotation;
this.annotation = describer.annotation;
}
b.addUint32(template.version)
b.addUint32(describer.version)
.addUint16(this.members.length);
for (let i = 0; i < this.functions.length; i++)

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 Ahmed Kh. Zamil
* Copyright (c) 2017 - 2022 Ahmed Kh. Zamil
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -42,12 +42,6 @@ import AsyncBag from '../Core/AsyncBag.js';
import IRecord from '../Data/IRecord.js';
import TemplateType from './Template/TemplateType.js';
import DistributedResource from '../Net/IIP/DistributedResource.js';
import TypedList from '../Data/TypedList.js';
import { Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64, Float32, Float64 } from '../Data/ExtendedTypes.js';
import Record from '../Data/Record.js';
import TypedMap from '../Data/TypedMap.js';
import {RepresentationType, RepresentationTypeIdentifier} from '../Data/RepresentationType.js';
import FactoryEntry from './FactoryEntry.js';
import IEnum from '../Data/IEnum.js';
export class WH extends IEventHandler
@@ -69,8 +63,6 @@ export class WH extends IEventHandler
this.protocols = new KeyList();
this.typesFactory = this._getBuiltInTypes();
this._register("connected");
this._register("disconnected");
///this._urlRegex = /^(?:([\S]*):\/\/([^\/]*)\/?)/;
@@ -122,7 +114,7 @@ export class WH extends IEventHandler
}
get(path, attributes = null)//, parent = null, manager = null)
{
{
var rt = new AsyncReply();
// var self = this;
@@ -348,7 +340,7 @@ export class WH extends IEventHandler
if (className.startsWith("E_"))
className = className.substr(2);
className = type.template.namespace + "." + className;
className = type.template.namespace + "." + (type.template.className ?? className);
var templates = this.templates.get(templateType);
@@ -358,7 +350,7 @@ export class WH extends IEventHandler
if (templates.at(i).className == className)
return templates.at(i);
var template = new TypeTemplate(type, this);
var template = new TypeTemplate(type, true);
return template;
}
@@ -434,8 +426,6 @@ export class WH extends IEventHandler
query(path)
{
var p = path.trim().split('/');
var resource;
@@ -543,92 +533,14 @@ export class WH extends IEventHandler
// }
return rt;
}
}
/**
* @param {Function} instanceCreator - creator
* @param {RepresentationType} representationType - type
*/
_getTypeEntries(type, representationType) {
let listType = TypedList.of(type);
var entry = new FactoryEntry(type, representationType);
let nullableEntry = new FactoryEntry(entry.nullableType, representationType.toNullable());
let listEntry = new FactoryEntry(listType,
new RepresentationType(RepresentationTypeIdentifier.TypedList, false,
null, [representationType]));
let nullableList = new FactoryEntry(listEntry.nullableType,
new RepresentationType(RepresentationTypeIdentifier.TypedList, true, null,
[representationType]));
let nullableItemListType = TypedList.of(entry.nullableType);
let listNullableItemEntry = new FactoryEntry(nullableItemListType,
new RepresentationType(RepresentationTypeIdentifier.TypedList, false,
null, [representationType.toNullable()]));
let nullableListNullableItemEntry = new FactoryEntry(nullableItemListType,
new RepresentationType(RepresentationTypeIdentifier.TypedList, true, null,
[representationType.toNullable()]));
return [
entry, nullableEntry, listEntry, nullableList, listNullableItemEntry, nullableListNullableItemEntry
];
}
/**
* @param {Function} instanceCreator - creator
* @param {RepresentationType} representationType - type
*/
defineType(type, representationType) {
var entries = this._getTypeEntries(type, representationType);
for(var e of entries)
this.typesFactory[e.type] = e; //.push(e);
}
_getBuiltInTypes() {
let entries = [
...this._getTypeEntries(Int8, new RepresentationType(RepresentationTypeIdentifier.Int8, false)),
...this._getTypeEntries(UInt8, new RepresentationType(RepresentationTypeIdentifier.UInt8, false)),
...this._getTypeEntries(Int16, new RepresentationType(RepresentationTypeIdentifier.Int16, false)),
...this._getTypeEntries(UInt16, new RepresentationType(RepresentationTypeIdentifier.UInt16, false)),
...this._getTypeEntries(Int32, new RepresentationType(RepresentationTypeIdentifier.Int32, false)),
...this._getTypeEntries(UInt32, new RepresentationType(RepresentationTypeIdentifier.UInt32, false)),
...this._getTypeEntries(Int64, new RepresentationType(RepresentationTypeIdentifier.Int64, false)),
...this._getTypeEntries(UInt64, new RepresentationType(RepresentationTypeIdentifier.UInt64, false)),
...this._getTypeEntries(Float32, new RepresentationType(RepresentationTypeIdentifier.Float32, false)),
...this._getTypeEntries(Float64, new RepresentationType(RepresentationTypeIdentifier.Float64, false)),
...this._getTypeEntries(String, new RepresentationType(RepresentationTypeIdentifier.String, String)),
...this._getTypeEntries(Date, new RepresentationType(RepresentationTypeIdentifier.DateTime, Date)),
...this._getTypeEntries(Record, new RepresentationType(RepresentationTypeIdentifier.Record, false)),
...this._getTypeEntries(IResource, new RepresentationType(RepresentationTypeIdentifier.Resource, false)),
...this._getTypeEntries(Array, new RepresentationType(RepresentationTypeIdentifier.List, false)),
...this._getTypeEntries(Map, new RepresentationType(RepresentationTypeIdentifier.Map, false)),
//...this._getTypeEntries(IResource, new RepresentationType(RepresentationTypeIdentifier.Resource, false)),
//...this._getTypeEntries(TypedMap, new RepresentationType(RepresentationTypeIdentifier.Resource, false)),
...this._getTypeEntries(TypedMap.of(String, Object), new RepresentationType(RepresentationTypeIdentifier.TypedMap, false, null, [
new RepresentationType(RepresentationTypeIdentifier.String, false),
RepresentationType.Dynamic])),
...this._getTypeEntries(TypedMap.of(UInt8, Object), new RepresentationType(RepresentationTypeIdentifier.TypedMap, false, null, [
new RepresentationType(RepresentationTypeIdentifier.UInt8, false),
RepresentationType.Dynamic])),
...this._getTypeEntries(TypedMap.of(Int32, Object), new RepresentationType(RepresentationTypeIdentifier.TypedMap, false, null, [
new RepresentationType(RepresentationTypeIdentifier.Int32, false),
RepresentationType.Dynamic])),
];
let rt = {};
for(let entry of entries)
rt[entry.type] = entry;
return rt;
defineType(type){
let template = this.getTemplateByType(type);
if (template == null)
throw new Error("Unsupported type.");
this.putTemplate(template);
}
}