Skip to content

Commit

Permalink
[js] support js require
Browse files Browse the repository at this point in the history
  • Loading branch information
nadako committed May 1, 2014
1 parent 1fe9757 commit 80b2897
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ module Meta = struct
| Internal
| IsVar
| JavaNative
| JsRequire
| Keep
| KeepInit
| KeepSub
Expand Down
1 change: 1 addition & 0 deletions common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ module MetaInfo = struct
| Meta.Internal -> ":internal",("Generates the annotated field/class with 'internal' access",[Platforms [Java;Cs]; UsedOnEither[TClass;TEnum;TClassField]])
| IsVar -> ":isVar",("Forces a physical field to be generated for properties that otherwise would not require one",[UsedOn TClassField])
| JavaNative -> ":javaNative",("Automatically added by -java-lib on classes generated from JAR/class files",[Platform Java; UsedOnEither[TClass;TEnum]; Internal])
| JsRequire -> ":jsRequire",("Generate javascript module require expression for given extern",[Platform Js; UsedOn TClass])
| Keep -> ":keep",("Causes a field or type to be kept by DCE",[])
| KeepInit -> ":keepInit",("Causes a class to be kept by DCE even if all its field are removed",[UsedOn TClass])
| KeepSub -> ":keepSub",("Extends @:keep metadata to all implementing and extending classes",[UsedOn TClass])
Expand Down
24 changes: 23 additions & 1 deletion genjs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,25 @@ let generate_static ctx (c,f,e) =
gen_value ctx e;
newline ctx

let generate_require ctx c =
let _, args, mp = Meta.get Meta.JsRequire c.cl_meta in
let p = (s_path ctx c.cl_path) in

if ctx.js_flatten then
spr ctx "var "
else
generate_package_create ctx c.cl_path;

(match args with
| [(EConst(String(module_name)),_)] ->
print ctx "%s = require(\"%s\")" p module_name
| [(EConst(String(module_name)),_) ; (EConst(String(object_path)),_)] ->
print ctx "%s = require(\"%s\").%s" p module_name object_path
| _ ->
error "Unsupported @:jsRequire format" mp);

newline ctx

let generate_type ctx = function
| TClassDecl c ->
(match c.cl_init with
Expand All @@ -1116,6 +1135,8 @@ let generate_type ctx = function
if p = "Math" then generate_class___name__ ctx c;
if not c.cl_extern then
generate_class ctx c
else if Meta.has Meta.JsRequire c.cl_meta then
generate_require ctx c
else if not ctx.js_flatten && Meta.has Meta.InitPackage c.cl_meta then
(match c.cl_path with
| ([],_) -> ()
Expand Down Expand Up @@ -1161,7 +1182,8 @@ let alloc_ctx com =
ctx.type_accessor <- (fun t ->
let p = t_path t in
match t with
| TClassDecl { cl_extern = true }
| TClassDecl ({ cl_extern = true } as c) when not (Meta.has Meta.JsRequire c.cl_meta)
-> dot_path p
| TEnumDecl { e_extern = true }
-> dot_path p
| _ -> s_path ctx p);
Expand Down

0 comments on commit 80b2897

Please sign in to comment.