Skip to content
Merged
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
85 changes: 42 additions & 43 deletions src/generators/cpp/cppRetyper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1762,6 +1762,47 @@ let native_field_name_remap field =
| None ->
keyword_remap field.cf_name

(* Generate a no op tfunc for an abstract function. *)
(* This allows it to go through the rest of the generators with no special cases. *)
(* We can't implement abstract functions as pure virtual due to cppia needing to construct the class. *)
let abstract_tfunc basic field =
let args, ret =
match follow_with_coro field.cf_type with
| Coro (t, r) -> Common.expand_coro_type basic t r
| NotCoro TFun (t, r) -> t, r
| _ -> cpp_abort InternalError field.cf_pos
in

(* We need to fetch the default values for abstract functions from the @:Value meta *)
let get_default_value name =
try
match Meta.get Meta.Value field.cf_meta with
| _, [ (EObjectDecl decls, _) ], _ ->
Some
(decls
|> List.find (fun ((n, _, _), _) -> n = name)
|> snd
|> type_constant_value basic)
| _ -> None
with Not_found -> None
in

let map_arg (name, _, t) =
( (alloc_var VGenerated name t null_pos), (get_default_value name) ) in
let expr =
match follow ret with
| TAbstract ({ a_path = ([], "Void") }, _) ->
{ eexpr = TReturn None; etype = ret; epos = null_pos }
| _ ->
let zero_val = Some { eexpr = TConst (TInt Int32.zero); etype = ret; epos = null_pos } in
{ eexpr = TReturn zero_val; etype = ret; epos = null_pos } in

{
tf_args = args |> List.map map_arg;
tf_type = ret;
tf_expr = expr;
}

let rec tcpp_class_from_tclass ctx ids slots class_def class_params =
let scriptable = Gctx.defined ctx.ctx_common Define.Scriptable in
let cpp_type_of = cpp_type_of ctx.ctx_common.basic in
Expand Down Expand Up @@ -1818,49 +1859,7 @@ let rec tcpp_class_from_tclass ctx ids slots class_def class_params =
| Method (MethNormal | MethInline), Some { eexpr = TFunction func } ->
Some (create_function field func)
| Method MethNormal, _ when has_class_field_flag field CfAbstract ->
(* We need to fetch the default values for abstract functions from the @:Value meta *)
let abstract_tfunc =
let args, ret =
match follow_with_coro field.cf_type with
| Coro (t, r) -> Common.expand_coro_type ctx.ctx_common.basic t r
| NotCoro TFun (t, r) -> t, r
| _ -> cpp_abort InternalError field.cf_pos
in

let get_default_value name =
try
match Meta.get Meta.Value field.cf_meta with
| _, [ (EObjectDecl decls, _) ], _ ->
Some
(decls
|> List.find (fun ((n, _, _), _) -> n = name)
|> snd
|> type_constant_value ctx.ctx_common.basic)
| _ -> None
with Not_found -> None
in

(* Generate a no op tfunc for our abstract *)
(* This allows it to go through the rest of the generator with no special cases *)
(* We can't implement abstract functions as pure virtual due to cppia needing to construct the class *)
let map_arg (name, _, t) =
( (alloc_var VGenerated name t null_pos), (get_default_value name) ) in
let expr =
match follow ret with
| TAbstract ({ a_path = ([], "Void") }, _) ->
{ eexpr = TReturn None; etype = ret; epos = null_pos }
| _ ->
let zero_val = Some { eexpr = TConst (TInt Int32.zero); etype = ret; epos = null_pos } in
{ eexpr = TReturn zero_val; etype = ret; epos = null_pos } in

{
tf_args = args |> List.map map_arg;
tf_type = ret;
tf_expr = expr;
}
in

Some (create_function field abstract_tfunc)
Some (create_function field (abstract_tfunc ctx.ctx_common.basic field))
| _ ->
None
else
Expand Down
12 changes: 10 additions & 2 deletions src/generators/cpp/gen/cppCppia.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1828,6 +1828,14 @@ let generate_script_class common_ctx script class_def =
+ match TClass.get_cl_init class_def with Some _ -> 1 | _ -> 0)
^ "\n");

(* Methods without an expression (i.e. abstract ones) still need a real function, because hxcpp
reads every class method as a ScriptCallable and the JIT accesses it as one *)
let method_expression field =
match field.cf_expr with
| None when not (has_class_flag class_def CInterface) ->
Some (mk (TFunction (CppRetyper.abstract_tfunc common_ctx.Gctx.basic field)) field.cf_type field.cf_pos)
| e -> e
in
let generate_field isStatic field =
match (field.cf_kind, follow field.cf_type) with
| Var { v_read = AccInline; v_write = AccNever }, _ ->
Expand All @@ -1854,15 +1862,15 @@ let generate_script_class common_ctx script class_def =
| Method MethDynamic, TFun (args, ret) ->
script#func isStatic true field.cf_name ret args
(has_class_flag class_def CInterface)
field.cf_expr field.cf_pos
(method_expression field) field.cf_pos
| Method _, TFun (args, ret) when field.cf_name = "new" ->
script#func true false "new"
(TInst (class_def, []))
args false field.cf_expr field.cf_pos
| Method _, TFun (args, ret) ->
script#func isStatic false field.cf_name ret args
(has_class_flag class_def CInterface)
field.cf_expr field.cf_pos
(method_expression field) field.cf_pos
| Method _, _ ->
print_endline
("Unknown method type "
Expand Down
Loading