Skip to content
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
7 changes: 5 additions & 2 deletions src/genes/Dependencies.hx
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,16 @@ class Dependencies {
return switch type {
case Abstract(name): name;
case Concrete(module, name, native):
if (native != null && native.indexOf('.') > -1)
if (native != null && native.indexOf('.') >= -1)
return native;
final deps = imports.get(module);
if (deps != null)
for (i in deps)
if (i.name == name)
return if (i.alias != null) i.alias else i.name;
return name;
return switch name {
case 'String' | 'Array' | 'Math': name;
case _: "$" + name;
}
}
}
4 changes: 2 additions & 2 deletions src/genes/Genes.hx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Genes {
case [module]:
final setup = [
for (sub in module.types)
macro js.Syntax.code($v{'var ${sub.name} = module.${sub.name}'})
macro js.Syntax.code($v{'var $$${sub.name} = module.${sub.name}'})
];

final list = [for (sub in module.types) macro $v{sub.fullname}];
Expand All @@ -83,7 +83,7 @@ class Genes {

for (i in 0...modules.length) {
for (sub in modules[i].types) {
setup.push(macro js.Syntax.code($v{'var ${sub.name} = modules[$i].${sub.name}'}));
setup.push(macro js.Syntax.code($v{'var $$${sub.name} = modules[$i].${sub.name}'}));
ignores.push(macro $v{sub.fullname});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/genes/dts/TypeEmitter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TypeEmitter {
params: Array<Type>, withConstraints = false) {
final write = writer.write, emitPos = writer.emitPos;
emitPos(type.pos);
write(writer.typeAccessor(type));
write(writer.typeAccessor(type).substr(1));
emitParams(writer, params, withConstraints);
}

Expand Down
75 changes: 49 additions & 26 deletions src/genes/es/ModuleEmitter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class ModuleEmitter extends ExprEmitter {
case {isStatic: true, isPublic: true}:
write('export const ');
emitIdent(field.name);
write(' = ');
write(' = $$');
emitIdent(TypeUtil.className(cl));
emitField(field.name);
writeNewline();
Expand All @@ -158,6 +158,7 @@ class ModuleEmitter extends ExprEmitter {
function emitStatic(cl: ClassType, field: Field) {
writeNewline();
emitPos(field.pos);
write('$$');
emitIdent(TypeUtil.className(cl));
emitField(field.name);
write(' = ');
Expand All @@ -168,7 +169,7 @@ class ModuleEmitter extends ExprEmitter {
writeNewline();
emitPos(field.pos);
write(ctx.typeAccessor(registerType));
write('.createStatic(');
write('.createStatic($$');
emitIdent(TypeUtil.className(cl));
write(', ');
emitString(field.name);
Expand Down Expand Up @@ -202,35 +203,28 @@ class ModuleEmitter extends ExprEmitter {

function emitInterface(cl: ClassType) {
writeNewline();
write('export const ');
write('const $$');
write(TypeUtil.className(cl));
write(' = {}');
writeNewline();
write('export { $$');
write(TypeUtil.className(cl));
write(' as ');
write(TypeUtil.className(cl));
write(' }');
writeNewline();
}

function emitClass(checkCycles: (module: String) -> Bool, cl: ClassType,
fields: Array<Field>, export = true) {
writeNewline();
emitComment(cl.doc);
emitPos(cl.pos);
if (export)
write('export ');

final id = cl.pack.concat([TypeUtil.className(cl)]).join('.');
if (id != 'genes.Register') {
write('const ');
write(TypeUtil.className(cl));
write(' = ');
writeGlobalVar("$hxClasses");
write('[');
emitString(id);
write(']');
write(' = ');
writeNewline();
}

emitPos(cl.pos);
write('class ');
write('class $$');
write(TypeUtil.className(cl));
if (cl.superClass != null || hasConstructor(fields)) {
write(' extends ');
Expand Down Expand Up @@ -358,7 +352,7 @@ class ModuleEmitter extends ExprEmitter {
write('get __class__() {');
increaseIndent();
writeNewline();
write('return ');
write('return $$');
emitIdent(TypeUtil.className(cl));
decreaseIndent();
writeNewline();
Expand All @@ -368,25 +362,36 @@ class ModuleEmitter extends ExprEmitter {
writeNewline();
write('}');

if (export)
if (export) {
writeNewline();
write('export { $$');
write(TypeUtil.className(cl));
write(' as ');
write(TypeUtil.className(cl));
write(' }');
writeNewline();
}

if (id != 'genes.Register') {
writeGlobalVar("$hxClasses");
write('[');
emitString(id);
write(']');
write(' = $$');
write(TypeUtil.className(cl));
writeNewline();
}
}

function emitEnum(et: EnumType) {
final id = et.pack.concat([et.name]).join('.');
writeNewline();
emitComment(et.doc);
emitPos(et.pos);
write('export const ');
write('const $$');
write(et.name);
write(' = ');
writeNewline();
writeGlobalVar("$hxEnums");
write('[');
emitString(id);
write(']');
write(' = ');
writeNewline();
write('{');
increaseIndent();
writeNewline();
Expand Down Expand Up @@ -428,10 +433,12 @@ class ModuleEmitter extends ExprEmitter {
write('}');
writeNewline();

write('$$');
write(et.name);
write('.__constructs__ = [');
for (c in join(et.names, write.bind(', '))) {
#if (haxe_ver >= 4.2)
write('$$');
write(et.name);
emitField(c);
#else
Expand All @@ -441,17 +448,33 @@ class ModuleEmitter extends ExprEmitter {
write(']');
writeNewline();

write('$$');
write(et.name);
write('.__empty_constructs__ = [');
final empty = [
for (name in et.names)
if (!et.constructs[name].type.match(TFun(_, _))) et.constructs[name]
];
for (c in join(empty, write.bind(', '))) {
write('$$');
write(et.name);
emitField(c.name);
}
write(']');
writeNewline();

write('export { $$');
write(et.name);
write(' as ');
write(et.name);
write(' }');
writeNewline();

writeGlobalVar("$hxEnums");
write('[');
emitString(id);
write(']');
write(' = $$');
write(et.name);
}
}
46 changes: 34 additions & 12 deletions tests/TestReservedClassNames.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package tests;

import tests.ExternalClass;
#if (haxe >= version("4.1.0"))
import Std.isOfType;
#else
import Std.is as isOfType;
#end

class Reserved extends Object {}

Expand All @@ -17,23 +22,40 @@ class TestReservedClassNames {
}

public function testGlobalType() {
/*
asserts.assert(Object != cast js.lib.Object); // class Object is defined in another module
asserts.assert(Promise != cast js.lib.Promise); // class Promise is defined in current module
asserts.assert(Collator != cast js.lib.intl.Collator); // Collator is a sub-type of Intl `@:native("Intl.Collator")`
asserts.assert(Object != cast js.lib.Object); // class Object is defined in another module
asserts.assert(Promise != cast js.lib.Promise); // class Promise is defined in current module
asserts.assert(Collator != cast js.lib.intl.Collator); // Collator is a sub-type of Intl `@:native("Intl.Collator")`

final nativePromise: Dynamic = js.lib.Promise; // we can actually reference the native Promise type
asserts.assert(Type.getClassName(nativePromise) == null); // no "haxe class name" for native types
asserts.assert(nativePromise.name == 'Promise'); // every js function has a `name`
final nativePromise: Dynamic = js.lib.Promise; // we can actually reference the native Promise type
asserts.assert(Type.getClassName(nativePromise) == null); // no "haxe class name" for native types
asserts.assert(nativePromise.name == 'Promise'); // every js function has a `name`

final customPromise: Dynamic = Promise; // we can actually reference the custom Promise type
asserts.assert(Type.getClassName(customPromise) == 'tests.Promise');
asserts.assert(customPromise.name == 'Promise'); // every js function has a `name`
*/
final customPromise: Dynamic = Promise; // we can actually reference the custom Promise type
asserts.assert(Type.getClassName(customPromise) == 'tests.Promise');
// asserts.assert(customPromise.name == 'Promise'); // every js function has a `name`

final nativePromiseInst: Dynamic = Promise.native();
trace(nativePromiseInst);
asserts.assert(isOfType(nativePromiseInst, nativePromise));

final customPromiseInst: Dynamic = Promise.custom();
trace(customPromiseInst);
asserts.assert(isOfType(customPromiseInst, customPromise));

return asserts.done();
}
}

class Promise {}
class Promise {
public function new() {}

@:keep public static function native(): js.lib.Promise<Dynamic> {
return new js.lib.Promise((res, rej) -> {});
}

@:keep public static function custom(): Promise {
return new Promise();
}
}

class Collator {}