Skip to content

Commit 843187f

Browse files
committed
Allow @:js.import(define)
1 parent 02d3313 commit 843187f

5 files changed

Lines changed: 66 additions & 28 deletions

File tree

src/generators/genjs.ml

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,37 +1668,61 @@ let generate js_gen com =
16681668
Gctx.map_source_header com.defines (fun s -> print ctx "// %s\n" s);
16691669

16701670
let import_statements = ref [] in
1671-
let () =
1672-
List.iter (fun mt -> match mt with
1673-
| TClassDecl c when (has_class_flag c CExtern) && Meta.has Meta.JsImport c.cl_meta && is_directly_used ctx.com c.cl_meta ->
1674-
let _, args, mp = Meta.get Meta.JsImport c.cl_meta in
1675-
let id = snd c.cl_path in
1676-
let flat_path = Path.flat_path (get_generated_class_path c) in
1677-
let alias = try
1678-
fst (Native.get_native_name c.cl_meta)
1679-
with Not_found ->
1680-
c.cl_meta <- (Meta.Native,[EConst (String(flat_path,SDoubleQuotes)),null_pos],null_pos) :: c.cl_meta;
1681-
Native.apply_native_paths mt;
1682-
flat_path in
1683-
(match args with
1671+
List.iter (fun mt -> match mt with
1672+
| TClassDecl c when (has_class_flag c CExtern) && Meta.has Meta.JsImport c.cl_meta && is_directly_used ctx.com c.cl_meta ->
1673+
let _, args, mp = Meta.get Meta.JsImport c.cl_meta in
1674+
let id = snd c.cl_path in
1675+
let flat_path = Path.flat_path (get_generated_class_path c) in
1676+
let alias = try
1677+
fst (Native.get_native_name c.cl_meta)
1678+
with Not_found ->
1679+
c.cl_meta <- (Meta.Native,[EConst (String(flat_path,SDoubleQuotes)),null_pos],null_pos) :: c.cl_meta;
1680+
Native.apply_native_paths mt;
1681+
flat_path
1682+
in
1683+
let err () =
1684+
abort ("Unsupported @:js.import format. Use:\n" ^
1685+
" @:js.import('mylib.js')\n" ^
1686+
" @:js.import('mylib.js', 'name')\n" ^
1687+
" @:js.import(@default 'mylib.js')\n" ^
1688+
" @:js.import(@star 'mylib.js')\n" ^
1689+
"'mylib.js' can also be replaced with mylibjs_path custom define,\nif you set it as -D mylibjs_path=\"mylib.js\"")
1690+
mp
1691+
in
1692+
let get_module_name expr =
1693+
match expr with
1694+
| EConst(String(module_name,_)),_ -> module_name
1695+
| EConst(Ident(define)),_ ->
1696+
begin try
1697+
let s = Define.raw_defined_value com.defines define in
1698+
Helper.unquote s
1699+
with Not_found ->
1700+
abort ("Define " ^ define ^ " with js library path is not specified") mp;
1701+
end
1702+
| _ -> err()
1703+
in
1704+
(match args with
16841705
(* @:js.import(@star "module") - namespace import *)
1685-
| [EMeta ((Meta.Custom "star",[],_),(EConst(String(module_name,_)),_)),_] ->
1706+
| [EMeta ((Meta.Custom "star",[],_),ename),_] ->
1707+
let module_name = get_module_name ename in
16861708
import_statements := (Printf.sprintf "import * as %s from \"%s\";" alias module_name) :: !import_statements
16871709
(* @:js.import(@default "module") - default import *)
1688-
| [EMeta ((Meta.Custom "default",[],_),(EConst(String(module_name,_)),_)),_] ->
1710+
| [EMeta ((Meta.Custom "default",[],_),ename),_] ->
1711+
let module_name = get_module_name ename in
16891712
import_statements := (Printf.sprintf "import %s from \"%s\";" alias module_name) :: !import_statements
16901713
(* @:js.import("module") - named import using class name *)
1691-
| [(EConst(String(module_name,_)),_)] ->
1714+
| [ename] ->
1715+
let module_name = get_module_name ename in
16921716
import_statements := (Printf.sprintf "import { %s as %s } from \"%s\";" id alias module_name) :: !import_statements
16931717
(* @:js.import("module", "exportName") - named import with alias *)
1694-
| [(EConst(String(module_name,_)),_); (EConst(String(export_name,_)),_)] ->
1718+
| [ename; (EConst(String(export_name,_)),_)] ->
1719+
let module_name = get_module_name ename in
16951720
import_statements := (Printf.sprintf "import { %s as %s } from \"%s\";" export_name alias module_name) :: !import_statements
16961721
| exprs ->
1697-
abort "Unsupported @:js.import format. Use: @:js.import('module'), @:js.import('module', 'name'), @:js.import(@default 'module'), or @:js.import(@star 'module')" mp
1698-
)
1699-
| _ -> ()
1700-
) com.types;
1701-
in
1722+
err ()
1723+
)
1724+
| _ -> ()
1725+
) com.types;
17021726
(match !import_statements with
17031727
| [] -> ()
17041728
| lines ->

tests/misc/projects/Issue10615/compile.hxml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
--class-path src
55
--js bin/main.js
66
--cmd node bin/main
7+
-D pixijs_path="../pixi.js"

tests/misc/projects/Issue10615/compile2.hxml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
--js bin/main.js
66
--cmd node bin/main
77
-D js-es=6
8+
-D pixijs_path="../pixi.js"

tests/misc/projects/Issue10615/src/Main.hx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import pixi.Pixi.PixiApp;
21
import pixi.Pixi.PixiDef;
32
import pixi.Pixi.Assets;
43
import pixi.Pixi.Application;
4+
import pixi.Pixi.Application2;
5+
import pixi.Pixi.Application3;
56

67
@:js.import(@star '../lib.js')
78
extern class Lib {
@@ -62,11 +63,15 @@ class Main {
6263
eq(app.test(), "test");
6364
eq(Application.name(), "Application");
6465

66+
var app = new Application2();
67+
eq(app.test(), "test");
68+
eq(Application2.name(), "Application");
69+
6570
eq(PixiDef.name(), "default name");
6671

67-
var app = new PixiApp();
72+
var app = new Application3();
6873
eq(app.test(), "test");
69-
eq(PixiApp.name(), "Application");
74+
eq(Application3.name(), "Application");
7075
new Main();
7176
}
7277

tests/misc/projects/Issue10615/src/pixi/Pixi.hx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,25 @@ extern class Application {
77
static function name():String;
88
}
99

10-
@:js.import(@default "../pixi.js")
11-
extern class PixiDef {
10+
@:js.import(pixijs_path, "Application")
11+
extern class Application2 {
12+
function new();
13+
function test():String;
1214
static function name():String;
1315
}
1416

1517
@:js.import("../pixi.js", "Application")
16-
extern class PixiApp {
18+
extern class Application3 {
1719
function new();
1820
function test():String;
1921
static function name():String;
2022
}
2123

24+
@:js.import(@default "../pixi.js")
25+
extern class PixiDef {
26+
static function name():String;
27+
}
28+
2229
@:js.import("../pixi.js", "Assets")
2330
extern class Assets {
2431
static function load<T>(url:String):String;

0 commit comments

Comments
 (0)