BIG FORMATING COMMIT
This commit is contained in:
@@ -2,44 +2,43 @@ package macros;
|
||||
|
||||
import haxe.macro.Context;
|
||||
import haxe.macro.Expr;
|
||||
|
||||
using Lambda;
|
||||
|
||||
|
||||
class DCEHack {
|
||||
public static final classes:Array<haxe.macro.Type> = [];
|
||||
|
||||
public static final classes: Array<haxe.macro.Type> = [];
|
||||
|
||||
macro static public function dceInclude(): Array<Field> {
|
||||
#if !display
|
||||
var localClass = Context.getLocalClass();
|
||||
macro static public function dceInclude():Array<Field> {
|
||||
#if !display
|
||||
var localClass = Context.getLocalClass();
|
||||
|
||||
if (localClass == null){
|
||||
return Context.getBuildFields();
|
||||
}
|
||||
if (localClass == null) {
|
||||
return Context.getBuildFields();
|
||||
}
|
||||
|
||||
// Ignore abstract classes
|
||||
if (localClass.get().isAbstract){
|
||||
return Context.getBuildFields();
|
||||
}
|
||||
// Ignore abstract classes
|
||||
if (localClass.get().isAbstract) {
|
||||
return Context.getBuildFields();
|
||||
}
|
||||
|
||||
classes.push(Context.getLocalType());
|
||||
#end
|
||||
return Context.getBuildFields();
|
||||
}
|
||||
classes.push(Context.getLocalType());
|
||||
#end
|
||||
return Context.getBuildFields();
|
||||
}
|
||||
|
||||
macro static public function dceGenerateCreate(){
|
||||
var exprs = [];
|
||||
macro static public function dceGenerateCreate() {
|
||||
var exprs = [];
|
||||
|
||||
for (c in classes){
|
||||
switch (c){
|
||||
case TInst(_.get() => t, _):
|
||||
var path: TypePath = {pack: t.pack, name: t.name};
|
||||
exprs.push(macro new $path());
|
||||
default:
|
||||
Context.error("Unknown type: " + c, Context.currentPos());
|
||||
}
|
||||
}
|
||||
for (c in classes) {
|
||||
switch (c) {
|
||||
case TInst(_.get() => t, _):
|
||||
var path:TypePath = {pack: t.pack, name: t.name};
|
||||
exprs.push(macro new $path());
|
||||
default:
|
||||
Context.error("Unknown type: " + c, Context.currentPos());
|
||||
}
|
||||
}
|
||||
|
||||
return macro return $a{exprs};
|
||||
}
|
||||
return macro return $a{exprs};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,76 +2,78 @@ package macros;
|
||||
|
||||
import haxe.macro.Context;
|
||||
import haxe.macro.Expr;
|
||||
|
||||
using Lambda;
|
||||
|
||||
class Exporter {
|
||||
macro public static function buildExport():Array<haxe.macro.Expr.Field> {
|
||||
var fields = Context.getBuildFields();
|
||||
macro public static function buildExport():Array<haxe.macro.Expr.Field> {
|
||||
var fields = Context.getBuildFields();
|
||||
|
||||
var getExp = [];
|
||||
var getExp = [];
|
||||
|
||||
for (field in fields){
|
||||
if (field.meta == null) continue;
|
||||
|
||||
var s = "";
|
||||
for (meta in field.meta){
|
||||
if (meta.name == "export"){
|
||||
switch (meta.params[0].expr){
|
||||
case EConst(CString(s1)):
|
||||
s = s1;
|
||||
default:
|
||||
Context.error("Invalid export name", meta.pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (field in fields) {
|
||||
if (field.meta == null)
|
||||
continue;
|
||||
|
||||
if (s == "") continue;
|
||||
var s = "";
|
||||
for (meta in field.meta) {
|
||||
if (meta.name == "export") {
|
||||
switch (meta.params[0].expr) {
|
||||
case EConst(CString(s1)):
|
||||
s = s1;
|
||||
default:
|
||||
Context.error("Invalid export name", meta.pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch (field.kind){
|
||||
case FFun(f):
|
||||
var funName = field.name;
|
||||
switch (f.ret){
|
||||
case TPath(p):
|
||||
switch (p.name){
|
||||
case "Int":
|
||||
getExp.push( macro $v{s} => (i: Int) -> lib.exporter.Response.ValueType.Int(this.$funName()) );
|
||||
case "Float":
|
||||
getExp.push( macro $v{s} => (i: Int) -> lib.exporter.Response.ValueType.Float(this.$funName()) );
|
||||
case "Bool":
|
||||
getExp.push( macro $v{s} => (i: Int) -> lib.exporter.Response.ValueType.Bool(this.$funName()) );
|
||||
case "String":
|
||||
getExp.push( macro $v{s} => (i: Int) -> lib.exporter.Response.ValueType.String(this.$funName()) );
|
||||
default:
|
||||
Context.error("Only Int, Float, Bool and String can be exported", field.pos);
|
||||
}
|
||||
default:
|
||||
Context.error("Only functions returning a type can be exported", field.pos);
|
||||
}
|
||||
default:
|
||||
Context.error("Only functions can be exported", field.pos);
|
||||
}
|
||||
}
|
||||
if (s == "")
|
||||
continue;
|
||||
|
||||
var exportField: Field = {
|
||||
name: "export",
|
||||
pos: Context.currentPos(),
|
||||
kind: FFun({
|
||||
args: [],
|
||||
ret: TPath({ name: "ExportConfig", pack: []}),
|
||||
expr: macro {
|
||||
return {
|
||||
getDelegates: $a{getExp},
|
||||
};
|
||||
}
|
||||
}),
|
||||
access: [APublic],
|
||||
doc: null,
|
||||
meta: [],
|
||||
};
|
||||
switch (field.kind) {
|
||||
case FFun(f):
|
||||
var funName = field.name;
|
||||
switch (f.ret) {
|
||||
case TPath(p):
|
||||
switch (p.name) {
|
||||
case "Int":
|
||||
getExp.push(macro $v{s} => (i:Int) -> lib.exporter.Response.ValueType.Int(this.$funName()));
|
||||
case "Float":
|
||||
getExp.push(macro $v{s} => (i:Int) -> lib.exporter.Response.ValueType.Float(this.$funName()));
|
||||
case "Bool":
|
||||
getExp.push(macro $v{s} => (i:Int) -> lib.exporter.Response.ValueType.Bool(this.$funName()));
|
||||
case "String":
|
||||
getExp.push(macro $v{s} => (i:Int) -> lib.exporter.Response.ValueType.String(this.$funName()));
|
||||
default:
|
||||
Context.error("Only Int, Float, Bool and String can be exported", field.pos);
|
||||
}
|
||||
default:
|
||||
Context.error("Only functions returning a type can be exported", field.pos);
|
||||
}
|
||||
default:
|
||||
Context.error("Only functions can be exported", field.pos);
|
||||
}
|
||||
}
|
||||
|
||||
fields.push(exportField);
|
||||
var exportField:Field = {
|
||||
name: "export",
|
||||
pos: Context.currentPos(),
|
||||
kind: FFun({
|
||||
args: [],
|
||||
ret: TPath({name: "ExportConfig", pack: []}),
|
||||
expr: macro {
|
||||
return {
|
||||
getDelegates: $a{getExp},
|
||||
};
|
||||
}
|
||||
}),
|
||||
access: [APublic],
|
||||
doc: null,
|
||||
meta: [],
|
||||
};
|
||||
|
||||
return fields;
|
||||
}
|
||||
fields.push(exportField);
|
||||
|
||||
return fields;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,78 +2,83 @@ package macros.rpc;
|
||||
|
||||
import haxe.macro.Context;
|
||||
import haxe.macro.Expr;
|
||||
|
||||
using Lambda;
|
||||
|
||||
class RPC {
|
||||
macro static public function buildRPC(): Array<Field> {
|
||||
var fields = Context.getBuildFields();
|
||||
macro static public function buildRPC():Array<Field> {
|
||||
var fields = Context.getBuildFields();
|
||||
|
||||
var className = Context.getLocalClass().get().name + "RPC";
|
||||
var className = Context.getLocalClass().get().name + "RPC";
|
||||
|
||||
var c = macro class $className extends macros.rpc.RPCBase {
|
||||
public function new(id: kernel.net.Package.NetworkID) {
|
||||
super(id,$v{className});
|
||||
}
|
||||
}
|
||||
var c = macro class $className extends macros.rpc.RPCBase {
|
||||
public function new(id:kernel.net.Package.NetworkID) {
|
||||
super(id, $v{className});
|
||||
}
|
||||
}
|
||||
|
||||
for (field in fields){
|
||||
if (field.meta == null) continue;
|
||||
if (field.meta.exists((i) -> i.name == "rpc") == false) continue;
|
||||
for (field in fields) {
|
||||
if (field.meta == null)
|
||||
continue;
|
||||
if (field.meta.exists((i) -> i.name == "rpc") == false)
|
||||
continue;
|
||||
|
||||
switch (field.kind){
|
||||
case FFun(f):
|
||||
c.fields.push({
|
||||
name: field.name,
|
||||
pos: field.pos,
|
||||
kind: FFun({
|
||||
args: f.args,
|
||||
expr: macro {
|
||||
return cast this._performRequest($v{field.name},[]);
|
||||
},
|
||||
ret: TPath({name: "Promise", params: [TPType(f.ret)], pack: ["tink","core"]}),
|
||||
}),
|
||||
access: [APublic],
|
||||
doc: null,
|
||||
meta: [],
|
||||
});
|
||||
default:
|
||||
Context.error("Only functions can be used for rpc", field.pos);
|
||||
}
|
||||
}
|
||||
switch (field.kind) {
|
||||
case FFun(f):
|
||||
c.fields.push({
|
||||
name: field.name,
|
||||
pos: field.pos,
|
||||
kind: FFun({
|
||||
args: f.args,
|
||||
expr: macro {
|
||||
return cast this._performRequest($v{field.name}, []);
|
||||
},
|
||||
ret: TPath({name: "Promise", params: [TPType(f.ret)], pack: ["tink", "core"]}),
|
||||
}),
|
||||
access: [APublic],
|
||||
doc: null,
|
||||
meta: [],
|
||||
});
|
||||
default:
|
||||
Context.error("Only functions can be used for rpc", field.pos);
|
||||
}
|
||||
}
|
||||
|
||||
haxe.macro.Context.defineType(c);
|
||||
haxe.macro.Context.defineType(c);
|
||||
|
||||
return fields;
|
||||
}
|
||||
return fields;
|
||||
}
|
||||
|
||||
macro static public function generateRPCPackageHandle() {
|
||||
var proto = Context.getLocalClass().get().name + "RPC";
|
||||
var exprs: Array<Expr> = [];
|
||||
macro static public function generateRPCPackageHandle() {
|
||||
var proto = Context.getLocalClass().get().name + "RPC";
|
||||
var exprs:Array<Expr> = [];
|
||||
|
||||
var fields = Context.getLocalClass().get().fields.get();
|
||||
var fields = Context.getLocalClass().get().fields.get();
|
||||
|
||||
for (field in fields){
|
||||
if (field.meta == null) continue;
|
||||
if (!field.meta.has("rpc")) continue;
|
||||
for (field in fields) {
|
||||
if (field.meta == null)
|
||||
continue;
|
||||
if (!field.meta.has("rpc"))
|
||||
continue;
|
||||
|
||||
switch (field.kind){
|
||||
case FMethod(k):
|
||||
var funName = field.name;
|
||||
exprs.push(macro {
|
||||
if (pack.data.func == $v{funName}){
|
||||
pack.respond(this.$funName());
|
||||
}
|
||||
});
|
||||
switch (field.kind) {
|
||||
case FMethod(k):
|
||||
var funName = field.name;
|
||||
exprs.push(macro {
|
||||
if (pack.data.func == $v{funName}) {
|
||||
pack.respond(this.$funName());
|
||||
}
|
||||
});
|
||||
|
||||
default:
|
||||
Context.error("Only functions can be used for rpc", field.pos);
|
||||
}
|
||||
}
|
||||
default:
|
||||
Context.error("Only functions can be used for rpc", field.pos);
|
||||
}
|
||||
}
|
||||
|
||||
return macro {
|
||||
kernel.net.Net.registerProto($v{proto},(pack)->{
|
||||
$a{exprs}
|
||||
});
|
||||
};
|
||||
}
|
||||
return macro {
|
||||
kernel.net.Net.registerProto($v{proto}, (pack) -> {
|
||||
$a{exprs}
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,24 +6,26 @@ import kernel.net.Package.NetworkID;
|
||||
using tink.CoreApi;
|
||||
|
||||
abstract class RPCBase {
|
||||
public final id:NetworkID;
|
||||
private final _proto:String;
|
||||
public function new(id: NetworkID, proto: String) {
|
||||
this.id = id;
|
||||
this._proto = proto;
|
||||
}
|
||||
public final id:NetworkID;
|
||||
|
||||
private function _performRequest(func: String, args: Array<Dynamic>):Promise<Dynamic> {
|
||||
return Net.sendAndAwait(id, this._proto, {
|
||||
func: func,
|
||||
// args: args
|
||||
}).map((res) -> {
|
||||
switch (res){
|
||||
case Success(pack):
|
||||
return pack.data;
|
||||
case Failure(_):
|
||||
return res;
|
||||
}
|
||||
});
|
||||
}
|
||||
private final _proto:String;
|
||||
|
||||
public function new(id:NetworkID, proto:String) {
|
||||
this.id = id;
|
||||
this._proto = proto;
|
||||
}
|
||||
|
||||
private function _performRequest(func:String, args:Array<Dynamic>):Promise<Dynamic> {
|
||||
return Net.sendAndAwait(id, this._proto, {
|
||||
func: func,
|
||||
// args: args
|
||||
}).map((res) -> {
|
||||
switch (res) {
|
||||
case Success(pack):
|
||||
return pack.data;
|
||||
case Failure(_):
|
||||
return res;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user