Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nightly Safe Macros #98

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/json2object/TypeUtils.hx
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,9 @@ class TypeUtils

return t;
}

public static function isAlive(ct:ComplexType, pos:Position):Type {
return try Context.resolveType(ct, pos) catch(e) null;
}
}
#end
23 changes: 12 additions & 11 deletions src/json2object/reader/DataBuilder.hx
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ typedef JsonType = {jtype:String, name:String, params:Array<Type>}
typedef ParserInfo = {packs:Array<String>, clsName:String}

class DataBuilder {

@:persistent
private static var counter = 0;
private static var parsers = new Map<String, Type>();
private static var parsers = new Map<String, Bool>();
private static var callPosition:Null<Position> = null;
private static var jcustom = ":jcustomparse";

Expand Down Expand Up @@ -936,9 +934,14 @@ class DataBuilder {
public static function makeParser(c:BaseType, type:Type, ?base:Type=null) {
if (base == null) { base = type; }

var parserMapName = base.toString();
if (parsers.exists(parserMapName)) {
return parsers.get(parserMapName);
var parserName = c.name + "_" + haxe.crypto.Md5.encode(base.toString());
var parser_cls = { name: parserName, pack: [], params: null, sub: null };

if (parsers.exists(parserName)) {
var resolved = TypeUtils.isAlive(TPath(parser_cls), Context.currentPos());
if (resolved != null) {
return resolved;
}
}

var defaultValueExpr:Expr = switch (type) {
Expand All @@ -955,7 +958,6 @@ class DataBuilder {
default: macro {};
}

var parserName = c.name + "_" + (counter++);
var parent = {name:"BaseParser", pack:["json2object", "reader"], params:[TPType(base.toComplexType())]};
var parser = macro class $parserName extends $parent {
public function new(?errors:Array<json2object.Error>=null, ?putils:json2object.PositionUtils=null, ?errorType:json2object.Error.ErrorType=json2object.Error.ErrorType.NONE) {
Expand Down Expand Up @@ -991,7 +993,6 @@ class DataBuilder {
});
}

var parser_cls = { name: parserName, pack: [], params: null, sub: null };
var getAutoExpr = macro return new $parser_cls([], putils, NONE).loadJson({value:JNull, pos:{file:"",min:0, max:1}});
var getAuto:Field = {
doc: null,
Expand Down Expand Up @@ -1080,9 +1081,9 @@ class DataBuilder {

haxe.macro.Context.defineType(parser);

var constructedType = haxe.macro.Context.getType(parserName);
parsers.set(parserMapName, constructedType);
return constructedType;
parsers.set(parserName, true);

return haxe.macro.Context.resolveType(TPath(parser_cls), Context.currentPos());

}

Expand Down
56 changes: 48 additions & 8 deletions src/json2object/utils/schema/DataBuilder.hx
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,43 @@ typedef Definitions = Map<String, JsonType>;

class DataBuilder {

static var BOOL = Context.getType("Bool");
static var FLOAT = Context.getType("Float");
static var STRING = Context.getType("String");
static var cachedBool:Type = null;
Aidan63 marked this conversation as resolved.
Show resolved Hide resolved
static var cachedFloat:Type = null;
static var cachedString:Type = null;

static var BOOL(get, never):Type;
static var FLOAT(get, never):Type;
static var STRING(get, never):Type;

static function get_BOOL() {
return switch cachedBool {
case null:
cachedBool = Context.getType("Bool");
case cached:
cached;
}
}

static function get_FLOAT() {
return switch cachedFloat {
case null:
cachedFloat = Context.getType("Float");
case cached:
cached;
}
}

static function get_STRING() {
return switch cachedString {
case null:
cachedString = Context.getType("String");
case cached:
cached;
}
}

static var counter:Int = 0;
private static var writers = new Map<String, Type>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh, it's weird that this wasn't being used at all

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, the schema writer only seemed to have the old counter and caching system half implemented.

@:persistent
private static var writers = new Map<String, Bool>();

private inline static function describe (type:JsonType, descr:Null<String>) {
return (descr == null) ? type : JTWithDescr(type, descr);
Expand Down Expand Up @@ -413,7 +444,15 @@ class DataBuilder {
}

static function makeSchemaWriter(c:BaseType, type:Type, parsingType:ParsingType) {
var swriterName = c.name + "_" + (counter++);
var swriterName = c.name + "_" + haxe.crypto.Md5.encode(type.toString() + Std.string(parsingType));
var swriter_cls = { name: swriterName, pack: [], params: null, sub: null };

if (writers.exists(swriterName)) {
var resolved = TypeUtils.isAlive(TPath(swriter_cls), Context.currentPos());
if (resolved != null) {
return resolved;
}
}

var definitions = new Definitions();
var obj = format(makeSchema(type, definitions), definitions, parsingType);
Expand All @@ -436,8 +475,9 @@ class DataBuilder {
}
haxe.macro.Context.defineType(schemaWriter);

var constructedType = haxe.macro.Context.getType(swriterName);
return haxe.macro.Context.getType(swriterName);
writers.set(swriterName, true);

return Context.resolveType(TPath(swriter_cls), Context.currentPos());
}

public static function build() {
Expand Down
22 changes: 12 additions & 10 deletions src/json2object/writer/DataBuilder.hx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ using json2object.utils.TypeTools;

class DataBuilder {
@:persistent
private static var counter = 0;
private static var writers = new Map<String, Type>();
private static var writers = new Map<String, Bool>();
private static var jcustom = ":jcustomwrite";

private static function notNull (type:Type) : Type {
Expand Down Expand Up @@ -383,12 +382,16 @@ class DataBuilder {
public static function makeWriter (c:BaseType, type:Type, base:Type) {
if (base == null) { base = type; }

var writerMapName = base.toString();
if (writers.exists(writerMapName)) {
return writers.get(writerMapName);
}
var writerName = c.name + "_" + haxe.crypto.Md5.encode(base.toString());
var writer_cls = { name: writerName, pack: [], params: null, sub: null };

var writerName = c.name + "_" + (counter++);
if (writers.exists(writerName)) {
var resolved = TypeUtils.isAlive(TPath(writer_cls), Context.currentPos());
if (resolved != null) {
return resolved;
}
}

var writerClass = macro class $writerName {
public var ignoreNullOptionals : Bool;
private var shouldQuote : Bool = true;
Expand Down Expand Up @@ -514,10 +517,9 @@ class DataBuilder {

haxe.macro.Context.defineType(writerClass);

var constructedType = haxe.macro.Context.getType(writerName);
writers.set(writerMapName, constructedType);
return constructedType;
writers.set(writerName, true);

return Context.resolveType(TPath(writer_cls), Context.currentPos());
}

public static function build() {
Expand Down
Loading