2
0
mirror of https://github.com/esiur/esiur-js.git synced 2025-06-27 07:13:12 +00:00
This commit is contained in:
2022-09-03 15:15:24 +03:00
parent 93ed4edcbd
commit 87e04c37ac
19 changed files with 162 additions and 565 deletions

View File

@ -54,6 +54,10 @@ export default class DC extends Uint8Array
}
static guidToBytes(value){
return value.value;
}
static boolToBytes(value)
{
var rt = new DC(1);

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

View File

@ -3,12 +3,12 @@ export default class Nullable {
static cache = { };
static getType(nullableType){
return nullableType.constructor.type;
return nullableType.constructor.underlyingType;
}
static of(type){
if (type.constructor.isNullable)
if (type.isNullable)
return type;
if (Nullable.cache[type] != null)
@ -16,7 +16,7 @@ export default class Nullable {
let c = class extends type{}
Object.defineProperty(c, "isNullable", {value: true});
Object.defineProperty(c, "type", {value: type});
Object.defineProperty(c, "underlyingType", {value: type});
Nullable.cache[type] = c;

View File

@ -110,6 +110,7 @@ TypeToIdentifierMap[Array] = RepresentationTypeIdentifier.List;
TypeToIdentifierMap[Map] = RepresentationTypeIdentifier.Map;
TypeToIdentifierMap[RecordArray] = RepresentationTypeIdentifier.RecordArray;
TypeToIdentifierMap[ResourceArray] = RepresentationTypeIdentifier.ResourceArray;
TypeToIdentifierMap[Object] = RepresentationTypeIdentifier.Dynamic;
const TupleIdentifierByLength = {
2: RepresentationTypeIdentifier.Tuple2,
@ -181,10 +182,10 @@ export default class RepresentationType {
if (type == null)
throw new Error("Type can't be null.");
let nullable = type.constructor.isNullable;
let nullable = type.isNullable;
if (nullable)
type = type.constructor.type; // original type
type = type.underlyingType; // original type
let identifier = TypeToIdentifierMap[type];
@ -208,19 +209,18 @@ export default class RepresentationType {
} else if (type.prototype instanceof TypedList) {
let elementType = RepresentationType.fromType(TypedList.getType(type));
let elementType = RepresentationType.fromType(type.elementType);
return new RepresentationType(RepresentationTypeIdentifier.TypedList, null, null, [elementType]);
} else if (type.prototype instanceof TypedMap) {
let subs = TypedMap.getTypes(type);
let keyType = RepresentationType.fromType(subs[0]);
let valueType = RepresentationType.fromType(subs[1]);
let keyType = RepresentationType.fromType(type.keyType);
let valueType = RepresentationType.fromType(type.valueType);
return new RepresentationType(RepresentationTypeIdentifier.TypedMap, null, null, [keyType, valueType]);
} else if (type.prototype instanceof Tuple) {
let subs = Tuple.gettypes(type).map(x=> RepresentationType.fromType(x));
let subs = type.subTypes.map(x => RepresentationType.fromType(x));
return new RepresentationType(TupleIdentifierByLength[subs.length], nullable, null, subs);
}

View File

@ -2,7 +2,7 @@ export default class Tuple extends Array {
static cache = {};
static getTypes(tuple){
return tuple.constructor.types;
return tuple.constructor.subTypes;
}
static of(){
@ -17,7 +17,7 @@ export default class Tuple extends Array {
let c = class extends Tuple{}
Object.defineProperty(c, "name", {value: types.map(x => x.name).join('') + "Tuple"});
Object.defineProperty(c, "types", {value: types});
Object.defineProperty(c, "subTypes", {value: types});
Tuple.cache[types] = c;

View File

@ -23,7 +23,7 @@ export default class TypedList extends Array
};
static getType(typedList){
return typedList.constructor.type;
return typedList.constructor.elementType;
}
static of(type){
@ -32,7 +32,7 @@ export default class TypedList extends Array
let c = class extends TypedList{}
Object.defineProperty(c, "name", {value: type.name + "List"});
Object.defineProperty(c, "type", {value: type});
Object.defineProperty(c, "elementType", {value: type});
TypedList.cache[type] = c;

View File

@ -56,16 +56,19 @@ export default class TemplateGenerator {
let rt = "";
let parentName;
let dependencies = [];
if (template.parentId != null) {
parentName = this._translateClassName(templates
let parentClassName = templates
.find((x) =>
(x.classId.valueOf() == template.parentId.valueOf()) &&
(x.type == TemplateType.Record))
.className);
rt += `class ${className} extends ${parentName} {\r\n`;
.className;
parentName = this._translateClassName(parentClassName);
dependencies.push(parentClassName);
rt += `export default class ${className} extends ${parentName} {\r\n`;
} else {
rt += `class ${className} extends IRecord { \r\n`;
rt += `export default class ${className} extends Esiur.Data.IRecord { \r\n`;
}
template.properties.forEach((p) => {
@ -76,36 +79,24 @@ export default class TemplateGenerator {
rt += "\r\n";
// rt += "deserialize(Map<String, dynamic> value) {");
// template.properties.forEach((p) {
// rt.writeln("${p.name} = value['${p.name}'];");
// });
//rt += "}\r\n";
// rt.writeln("Map<String, dynamic> serialize() {");
// rt.writeln("var rt = Map<String, dynamic>();");
// template.properties.forEach((p) {
// rt.writeln("rt['${p.name}'] = ${p.name};");
// });
// rt.writeln("return rt;");
// rt.writeln("}");
// add template
var descProps = template.properties
.map((p) => {
var ptTypeName = this.getTypeName(template, p.valueType, templates);
return `new Prop('${p.name}', ${ptTypeName}, ${this._escape(p.readAnnotation)}, ${this._escape(p.writeAnnotation)})`;
var ptTypeName = this.getTypeName(template, p.valueType, templates, dependencies);
return `new Esiur.Resource.Template.Prop('${p.name}', ${ptTypeName}, ${this._escape(p.readAnnotation)}, ${this._escape(p.writeAnnotation)})`;
});
rt += `\r\nstatic get template() {return new TemplateDescriber('${template.className}', [\r\n${descProps.join(',\r\n')}], \r\n${parentName}, ${template.version}, ${this.toLiteral(template.annotation)});\r\n}`;
let cls = template.className.split('.');
let namespace = cls.slice(0, cls.length - 1).join('.');
rt += `\r\nstatic get template() {return new Esiur.Resource.Template.TemplateDescriber('${namespace}', [\r\n${descProps.join(',\r\n')}], \r\n${parentName}, ${template.version}, ${this.toLiteral(template.annotation)}, Esiur.Data.Guid.parse('${template.classId.toString()}'), '${className}');\r\n}`;
rt += "\r\n}";
rt += `\r\nnew Esiur.Resource.Template.TypeTemplate(${className}, true);\r\n`
rt = this._getDependenciesImports(dependencies) + rt;
return rt;
}
@ -118,45 +109,54 @@ export default class TemplateGenerator {
return `/* ${this.getTypeName(forTemplate, representationType, templates)} */`;
}
static getTypeName(forTemplate, representationType, templates) {
static getTypeName(forTemplate, representationType, templates, dependencies) {
let name;
if (representationType.identifier == RepresentationTypeIdentifier.TypedResource) {
if (representationType.guid.valueOf() == forTemplate.classId.valueOf())
name = forTemplate.className.split('.').last;
else
name = this._translateClassName(templates
name = forTemplate.className.split('.').slice(-1)[0];
else {
let className = templates
.find((x) =>
x.classId.valueOf() == representationType.guid.valueOf() &&
(x.type == TemplateType.Resource ||
x.type == TemplateType.Wrapper))
.className);
.className;
if (!dependencies?.includes(className)) dependencies?.push(className);
name = this._translateClassName(className);
}
} else if (representationType.identifier == RepresentationTypeIdentifier.TypedRecord) {
if (representationType.guid.valueOf() == forTemplate.classId.valueOf())
name = forTemplate.className.split('.').last;
else
name = this._translateClassName(templates
name = forTemplate.className.split('.').slice(-1)[0];
else {
let className = templates
.find((x) =>
x.classId.valueOf() == representationType.guid.valueOf() &&
x.type == TemplateType.Record)
.className);
.className;
if (!dependencies?.includes(className)) dependencies?.push(className);
name = this._translateClassName(className);
}
} else if (representationType.identifier == RepresentationTypeIdentifier.Enum) {
if (representationType.guid.valueOf() == forTemplate.classId.valueOf())
name = forTemplate.className.split('.').last;
else
name = this._translateClassName(templates
name = forTemplate.className.split('.').slice(-1)[0];
else {
let className = templates
.find((x) =>
x.classId.valueOf() == representationType.guid.valueOf() &&
x.type == TemplateType.Enum)
.className);
.className;
if (!dependencies?.includes(className)) dependencies?.push(className);
name = this._translateClassName(className);
}
} else if (representationType.identifier == RepresentationTypeIdentifier.TypedList)
name = "TypedList.of(" + this.getTypeName(forTemplate, representationType.subTypes[0], templates) + ")";
name = "Esiur.Data.TypedList.of(" + this.getTypeName(forTemplate, representationType.subTypes[0], templates, dependencies) + ")";
else if (representationType.identifier ==
RepresentationTypeIdentifier.TypedMap)
name = "TypedMap.of(" +
this.getTypeName(forTemplate, representationType.subTypes[0], templates) +
name = "Esiur.Data.TypedMap.of(" +
this.getTypeName(forTemplate, representationType.subTypes[0], templates, dependencies) +
"," +
this.getTypeName(forTemplate, representationType.subTypes[1], templates) +
this.getTypeName(forTemplate, representationType.subTypes[1], templates, dependencies) +
")";
else if (representationType.identifier ==
RepresentationTypeIdentifier.Tuple2 ||
@ -165,7 +165,7 @@ export default class TemplateGenerator {
representationType.identifier == RepresentationTypeIdentifier.Tuple5 ||
representationType.identifier == RepresentationTypeIdentifier.Tuple6 ||
representationType.identifier == RepresentationTypeIdentifier.Tuple7)
name = "Tuple.of(" + representationType.subTypes.map( x => this.getTypeName(x, templates)).join(',') + ")";
name = "Esiur.Data.Tuple.of(" + representationType.subTypes.map( x => this.getTypeName(forTemplate, x, templates, dependencies)).join(',') + ")";
else {
switch (representationType.identifier) {
case RepresentationTypeIdentifier.Dynamic:
@ -286,10 +286,11 @@ export default class TemplateGenerator {
}
});
imports += "\r\n";
imports += "\r\n\r\n";
return imports;
};
// make sources
templates.forEach(async (tmp) => {
console.log(`Generating '${tmp.className}'.`);
@ -297,17 +298,29 @@ export default class TemplateGenerator {
var source = "";
if (tmp.type == TemplateType.Resource) {
source = makeImports(tmp) + this.generateClass(tmp, templates, asyncSetters);
source = this.generateClass(tmp, templates, asyncSetters);
} else if (tmp.type == TemplateType.Record) {
source = makeImports(tmp) + this.generateRecord(tmp, templates);
source = this.generateRecord(tmp, templates);
} else if (tmp.type == TemplateType.Enum) {
source = makeImports(tmp) + this.generateEnum(tmp, templates);
source = this.generateEnum(tmp, templates);
}
fs.writeFileSync(filePath, source);
});
// make module
let modulePath = `${dstDir}/init.g.js`;
let module = makeImports() + `\r\nlet module = {}; \r\n`;
templates.forEach((tmp) => {
let typeName = tmp.className.split('.').join('_');
module += `Esiur.define(module, ${typeName}, '${tmp.className}');\r\n`;
});
module += "\r\nexport default module;";
fs.writeFileSync(modulePath, module);
return dstDir;
@ -321,46 +334,63 @@ export default class TemplateGenerator {
}
static generateEnum(template, templates) {
var className = template.className.split('.').slice(-1)[0];
var rt = "";
let className = template.className.split('.').slice(-1)[0];
let rt = "";
let dependencies = [];
rt += `class ${className} extends IEnum {\r\n`;
rt += `export default class ${className} extends Esiur.Data.IEnum {\r\n`;
template.constants.forEach((c) => {
rt += `static ${className} ${c.name} = ${className}(${c.index}, ${c.value}, '${c.name}');\r\n`;
rt += `static ${c.name} = new ${className}(${c.index}, ${c.value}, '${c.name}');\r\n`;
});
rt += "\r\n";
rt += `${className}([int index = 0, value, String name = '']) : super(index, value, name);`;
// add template
var descConsts = template.constants.map((p) => {
var ctTypeName = this.getTypeName(template, p.valueType, templates);
return `Const('${p.name}', getTypeOf<${ctTypeName}>(), ${p.value}, ${this._escape(p.annotation)})`;
}).join(', ');
var ctTypeName = this.getTypeName(template, p.valueType, templates, dependencies);
return `new Esiur.Resource.Template.Const('${p.name}', ${ctTypeName}, ${p.value}, ${this._escape(p.annotation)})`;
});
rt += `TemplateDescriber get template => TemplateDescriber('${template.className}', constants: [${descConsts}], annotation: ${this.toLiteral(template.annotation)});`;
let cls = template.className.split('.');
let namespace = cls.slice(0, cls.length - 1).join('.');
rt += `\r\nstatic get template() {return new Esiur.Resource.Template.TemplateDescriber('${namespace}', [\r\n${descConsts.join(',\r\n')}], \r\n null, ${template.version}, ${this.toLiteral(template.annotation)}, Esiur.Data.Guid.parse('${template.classId.toString()}'), '${className}');\r\n}`;
rt += "\r\n}";
rt += `\r\nnew Esiur.Resource.Template.TypeTemplate(${className}, true);\r\n`
rt = this._getDependenciesImports(dependencies) + rt;
return rt;
}
static _getDependenciesImports(dependencies){
let rt = "";
dependencies.forEach(className => {
rt += `import ${className.split('.').join('_')} from './${className}.g.js';\r\n`;
});
return rt + "\r\n";
}
static generateClass(template, templates, asyncSetters = true) {
let className = template.className.split('.').slice(-1)[0];
let parentName = null;
let rt = "";
let dependencies = [];
if (template.parentId != null) {
parentName = this._translateClassName(templates
let parentClassName = templates
.find((x) =>
(x.classId.valueOf() == template.parentId.valueOf()) &&
(x.type == TemplateType.Resource ||
x.type == TemplateType.Wrapper))
.className);
.className;
parentName = this._translateClassName(parentClassName);
dependencies.push(parentClassName);
rt += `export default class ${className} extends ${parentName} {\r\n`;
} else {
rt += `export default class ${className} extends Esiur.Net.IIP.DistributedResource {\r\n`;
@ -374,6 +404,10 @@ export default class TemplateGenerator {
// rt += "}\r\n";
template.constants.forEach((c) => {
var ctTypeName = this.getTypeName(template, c.valueType, templates, dependencies);
rt += `static ${c.name} = new ${ctTypeName}(${c.value});\r\n`;
});
template.functions.filter((f) => !f.inherited).forEach((f) => {
var rtTypeName = this.getDecoratedTypeName(template, f.returnType, templates);
@ -382,7 +416,7 @@ export default class TemplateGenerator {
if (f.isStatic) {
//rt += `static AsyncReply<${rtTypeName}> ${f.name}(DistributedConnection connection`;
rt += `static ${rtTypeName} ${f.name}(connection`;
rt += `${rtTypeName} \r\n static ${f.name}(connection`;
if (positionalArgs.length > 0)
rt += `, ${positionalArgs.map((a) => this.getDecoratedTypeName(template, a.type, templates) + " " + a.name).join(',')}`;
@ -392,7 +426,7 @@ export default class TemplateGenerator {
}
} else {
//rt += `AsyncReply<${rtTypeName}> ${f.name}(`;
rt += `${rtTypeName} ${f.name}(`;
rt += `${rtTypeName} \r\n ${f.name}(`;
if (positionalArgs.length > 0)
rt += `${positionalArgs.map((a) => this.getDecoratedTypeName(template, a.type, templates) + " " + a.name).join(',')}`;
@ -407,8 +441,8 @@ export default class TemplateGenerator {
rt += ") {\r\n";
// var argsMap = new (TypedMap.of(UInt8, Object));
rt += "var args = new (Esiur.Data.TypedMap.of(Esiur.Data.UInt8, Object))(";
rt += `{${positionalArgs.map((e) => "new Esiur.Data.UInt8(" + e.index.toString() + ') :' + e.name).join(',')}});\r\n`;
rt += "var args = new (Esiur.Data.TypedMap.of(Esiur.Data.UInt8, Object))();\r\n";
rt += `${positionalArgs.map((e) => `args.set(new Esiur.Data.UInt8(${e.index.toString()}), ${e.name});`).join('\r\n')}\r\n`;
optionalArgs.forEach((a) => {
rt += `if (${a.name} != null) args.set(new Esiur.Data.UInt8(${a.index}), ${a.name});\r\n`;
@ -424,7 +458,7 @@ export default class TemplateGenerator {
rt += `.then((x) => rt.trigger(x))\r\n`;
rt += `.error((x) => rt.triggerError(x))\r\n`;
rt += `.chunk((x) => rt.triggerChunk(x));\r\n`;
rt += `return rt; }\r\n`;
rt += `return rt; \r\n}\r\n`;
});
template.properties.filter((p) => !p.inherited).forEach((p) => {
@ -449,16 +483,16 @@ export default class TemplateGenerator {
// add template
var descProps = template.properties //.where((p) => !p.inherited)
.map((p) => {
var ptTypeName = this.getTypeName(template, p.valueType, templates);
var ptTypeName = this.getTypeName(template, p.valueType, templates, dependencies);
return `new Esiur.Resource.Template.Prop('${p.name}', ${ptTypeName}, ${this._escape(p.readAnnotation)}, ${this._escape(p.writeAnnotation)})`;
});
var descFuncs = template.functions
.map((f) => {
var ftTypeName = this.getTypeName(template, f.returnType, templates);
var ftTypeName = this.getTypeName(template, f.returnType, templates, dependencies);
var args = f.args.map((a) => {
var atTypeName = this.getTypeName(template, a.type, templates);
var atTypeName = this.getTypeName(template, a.type, templates, dependencies);
return `new Esiur.Resource.Template.Arg('${a.name}', ${atTypeName}, ${a.optional})`;
}).join(', ');
@ -466,23 +500,30 @@ export default class TemplateGenerator {
});
var descEvents = template.events
//.where((e) => !e.inherited)
//.where((e) => !e.inherited) @REVIEW
.map((e) => {
var etTypeName = this.getTypeName(template, e.argumentType, templates);
var etTypeName = this.getTypeName(template, e.argumentType, templates, dependencies);
return `new Esiur.Resource.Template.Evt('${e.name}', ${etTypeName}, ${e.listenable}, ${this._escape(e.annotation)})`;
});
// constructor(namespace, members, parent, version = 0, annotation = null){
var descConsts = template.constants.map((p) => {
var ctTypeName = this.getTypeName(template, p.valueType, templates, dependencies);
return `new Esiur.Resource.Template.Const('${p.name}', ${ctTypeName}, ${p.value}, ${this._escape(p.annotation)})`;
});
let cls = template.className.split('.');
let namespace = cls.slice(0, cls.length - 1).join('.');
rt += `\r\nstatic get template() {return new Esiur.Resource.Template.TemplateDescriber('${namespace}', [\r\n${[...descProps, ...descFuncs, ...descEvents].join(',\r\n')}], \r\n${parentName}, ${template.version}, ${this.toLiteral(template.annotation)}, Esiur.Data.Guid.parse('${template.classId.toString()}'), '${className}');\r\n}`;
rt += `\r\nstatic get template() {return new Esiur.Resource.Template.TemplateDescriber('${namespace}', [\r\n${[...descProps, ...descFuncs, ...descEvents, ...descConsts].join(',\r\n')}], \r\n${parentName}, ${template.version}, ${this.toLiteral(template.annotation)}, Esiur.Data.Guid.parse('${template.classId.toString()}'), '${className}');\r\n}`;
rt += "\r\n}\r\n";
rt += `new Esiur.Resource.Template.TypeTemplate(${className}, true);\r\n`
rt += `\r\nnew Esiur.Resource.Template.TypeTemplate(${className}, true);\r\n`
rt = this._getDependenciesImports(dependencies) + rt;
return rt;
}
}

View File

@ -90,13 +90,15 @@ import { Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt1
import Tuple from './Data/Tuple.js';
import Nullable from './Data/Nullable.js';
import Void from './Data/Void.js';
import IEnum from './Data/IEnum.js';
import {TemplateDescriber, Prop, Func, Evt, Const, Arg} from './Resource/Template/TemplateDescriber.js';
const namespace = {
Core: { AsyncReply, AsyncException, AsyncQueue, ErrorType, ExceptionCode, IDestructible, IEventHandler, ProgressType},
Data: {AutoList, AutoMap, BinaryList, Codec, DC, TypedList, TypedMap, Guid, IRecord, KeyList, NotModified, ResourceArrayType,
PropertyValue, Record, ResourceArray, RepresentationType, RepresentationTypeIdentifier, TransmissionType, TransmissionTypeIdentifier,
Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Float32, Float64, Float128, Char16, Char8, Tuple,
Nullable, Void
Nullable, Void, IEnum
},
Net: {INetworkReceiver, NetworkBuffer, NetworkConnections, NetworkServer, NetworkSession, SendList,
IIP: {DistributedConnection, DistributedPropertyContext, DistributedResource, DistributedResourceQueueItem,
@ -109,7 +111,8 @@ const namespace = {
Resource: {CustomResourceEvent, Instance, IResource, IStore, Warehouse,
Template: {
ArgumentTemplate, EventTemplate, FunctionTemplate, MemberTemplate,
MemberType, PropertyTemplate, TemplateType, TypeTemplate
MemberType, PropertyTemplate, TemplateType, TypeTemplate,
TemplateDescriber, Prop, Func, Evt, Const, Arg
}
},
Security: {
@ -122,21 +125,19 @@ const namespace = {
Permissions: {ActionType, IPermissionsManager, Ruling},
},
Stores: {IndexedDBStore, MemoryStore},
Generated: {},
};
namespace.define = function(type, className) {
let sc = className.split('.');
let target = namespace.Generated;
for(let i = 0; i < sc.length; i++) {
if (target[sc[i]] == undefined)
target[sc[i]] = {};
target = target[sc[i]];
define: function(target, type, className) {
let sc = className.split('.');
for(let i = 0; i < sc.length; i++) {
if (target[sc[i]] == undefined)
target[sc[i]] = {};
target = target[sc[i]];
}
target[sc[sc.length - 1]] = type;
}
target[sc[sc.length - 1]] = type;
}
};
if (typeof window !== 'undefined')