ic_jbe.erl

来自「OTP是开放电信平台的简称」· ERL 代码 · 共 1,486 行 · 第 1/4 页

ERL
1,486
字号
    ic_codegen:emit(Fd, "    public ~sHolder(~s _arg) {\n",[InterfaceName, FullInterfaceName]),    ic_codegen:emit(Fd, "        value = _arg;\n"),    ic_codegen:emit(Fd, "    }\n\n"),    ic_codegen:emit(Fd, "    public void _marshal() {\n"),    ic_codegen:emit(Fd, "    }\n\n"),    ic_codegen:emit(Fd, "    public void _unmarshal() {\n"),    ic_codegen:emit(Fd, "    }\n\n"),	        ic_codegen:emit(Fd, "}\n\n").%%%-----------------------------------------------------%%%%%%  Helper file generation%%%%%%-----------------------------------------------------emit_helper(G, N, X, Fd) ->    InterfaceName = ic_forms:get_java_id(X),    FullInterfaceName = ic_util:to_dot([InterfaceName|N]),    ic_codegen:emit(Fd, "public final class ~sHelper {\n\n",[InterfaceName]),    ic_codegen:emit(Fd, "    // Constructor\n"),    ic_codegen:emit(Fd, "    public ~sHelper() {\n",[InterfaceName]),    ic_codegen:emit(Fd, "    }\n\n"),    ic_codegen:emit(Fd, "    public static void _marshal() {\n"),    ic_codegen:emit(Fd, "        // Writing the object to the message\n"),    ic_codegen:emit(Fd, "    }\n\n"),    ic_codegen:emit(Fd, "    public static ~s _unmarshal() {\n",[FullInterfaceName]),    ic_codegen:emit(Fd, "        // Reading the object from the message\n"),    ic_codegen:emit(Fd, "        return null;\n"),    ic_codegen:emit(Fd, "    }\n\n"),        ic_codegen:emit(Fd, "    public static java.lang.String id() {\n"),    ic_codegen:emit(Fd, "        return ~p;\n",[ictk:get_IR_ID(G, N, X)]),    ic_codegen:emit(Fd, "    }\n\n"),	        ic_codegen:emit(Fd, "}\n\n").%%%-----------------------------------------------------%%%%%%  Stub file generation%%%%%%-----------------------------------------------------emit_stub(G, N, X, Fd) ->    InterfaceName = ic_forms:get_java_id(X), %% Java Interface Name    IFCName = ic_forms:get_id2(X),           %% Internal Interface Name     FullInterfaceName = ic_util:to_dot([InterfaceName|N]),    Body = ic_forms:get_body(X),    ic_codegen:emit(Fd, "public class _~sStub implements ~s {\n\n",		    [InterfaceName,FullInterfaceName]),    ic_codegen:emit(Fd, "    // Client data\n"),    ic_codegen:emit(Fd, "    public ~sEnvironment _env;\n\n",[?ICPACKAGE]),    ic_codegen:emit(Fd, "    // Constructors\n"),    ic_codegen:emit(Fd, "    public _~sStub(~sOtpSelf _self,\n",[InterfaceName,?ERLANGPACKAGE]),    ic_codegen:emit(Fd, "                       ~sOtpPeer _peer,\n",[?ERLANGPACKAGE]),    ic_codegen:emit(Fd, "                       java.lang.Object _server) throws java.lang.Exception {\n\n"),        ic_codegen:emit(Fd, "      _env =\n"),    ic_codegen:emit(Fd, "         new ~sEnvironment(_self, _peer, _server);\n",[?ICPACKAGE]),    ic_codegen:emit(Fd, "      _env.connect();\n"),    ic_codegen:emit(Fd, "    }\n\n"),        ic_codegen:emit(Fd, "    public _~sStub(java.lang.String _selfN,\n",[InterfaceName]),    ic_codegen:emit(Fd, "                       java.lang.String _peerN,\n"),    ic_codegen:emit(Fd, "                       java.lang.String _cookie,\n"),    ic_codegen:emit(Fd, "                       java.lang.Object _server) throws java.lang.Exception {\n\n"),    ic_codegen:emit(Fd, "      _env =\n"),    ic_codegen:emit(Fd, "         new ~sEnvironment(_selfN, _peerN, _cookie, _server);\n",[?ICPACKAGE]),    ic_codegen:emit(Fd, "      _env.connect();\n"),    ic_codegen:emit(Fd, "    }\n\n"),    ic_codegen:emit(Fd, "    public _~sStub(~sOtpConnection _connection,\n",[InterfaceName, ?ERLANGPACKAGE]),    ic_codegen:emit(Fd, "                       java.lang.Object _server) throws java.lang.Exception {\n\n"),    ic_codegen:emit(Fd, "      _env =\n"),    ic_codegen:emit(Fd, "         new ~sEnvironment(_connection, _server);\n",[?ICPACKAGE]),    ic_codegen:emit(Fd, "      _env.connect();\n"),    ic_codegen:emit(Fd, "    }\n\n"),    emit_message_reference_extraction(Fd),    emit_servers_object_access(Fd),    emit_client_connection_close(Fd),    emit_client_connection_reconnect(Fd),    emit_client_destroy(Fd),    lists:foreach(fun({_Name, Body1}) ->			  emit_op_implementation(G, [IFCName|N], Body1, Fd) end,		  [{x, Body} | X#interface.inherit_body]),        ic_codegen:emit(Fd, "}\n\n").emit_op_implementation(G, N, [X |Xs], Fd) when record(X, op) ->    WireOpName = ic_forms:get_id2(X),    OpName = ic_forms:get_java_id(WireOpName),    {_, ArgNames, TypeList} = extract_info(G, N, X),    {R, ParamTypes, _} = TypeList,        RT = ic_java_type:getParamType(G,N,R,ret),    PL = ic_util:mk_list(gen_par_list(G, N, X, ParamTypes, ArgNames)),    CMCPL = ic_util:mk_list(gen_client_marshal_call_par_list(ArgNames)),    ic_codegen:emit(Fd, "    // Operation ~p implementation\n", [ic_util:to_colon([WireOpName|N])]),    ic_codegen:emit(Fd, "    public ~s ~s(~s)\n", [RT, OpName, PL]),    ic_codegen:emit(Fd, "      throws java.lang.Exception {\n\n"),    %% Function marshal call    ic_codegen:emit(Fd, "      // Calling the marshal function\n"),    case CMCPL of	"" ->	    ic_codegen:emit(Fd, "      _~s_marshal(_env);\n\n",[OpName]);	_ ->	    ic_codegen:emit(Fd, "      _~s_marshal(_env, ~s);\n\n",[OpName, CMCPL])    end,    %% Sending call    ic_codegen:emit(Fd, "      // Message send\n"),    ic_codegen:emit(Fd, "      _env.send();\n\n"),    case ic_forms:is_oneway(X) of	true ->	    ok;	false ->	    %% Receiving return values	    ic_codegen:emit(Fd, "      // Message receive\n"),	    ic_codegen:emit(Fd, "      _env.receive();\n\n"),	    	    %% Function unmarshal call	    case RT of		"void" ->		    case ic_util:mk_list(gen_client_unmarshal_call_par_list(ArgNames)) of			"" ->			    ic_codegen:emit(Fd, "      // Calling the unmarshal function\n"),			    ic_codegen:emit(Fd, "      _~s_unmarshal(_env);\n",					    [OpName]);			UMCPL ->			    ic_codegen:emit(Fd, "      // Calling the unmarshal function\n"),			    ic_codegen:emit(Fd, "      _~s_unmarshal(_env, ~s);\n",					    [OpName,UMCPL])		    end;		_ ->		    ic_codegen:emit(Fd, "      // Calling the unmarshal function\n"),		    case ic_util:mk_list(gen_client_unmarshal_call_par_list(ArgNames)) of			"" ->			    ic_codegen:emit(Fd, "      return _~s_unmarshal(_env);\n",					    [OpName]);			UMCPL ->			    ic_codegen:emit(Fd, "      return _~s_unmarshal(_env, ~s);\n",					    [OpName,UMCPL])		    end	    end    end,    ic_codegen:emit(Fd, "    }\n\n"),    %% Marshalling    emit_op_marshal(G, N, X, Fd),    %% UnMarshalling    emit_op_unmarshal(G, N, X, Fd),    ic_codegen:emit(Fd, "\n"),    emit_op_implementation(G, N, Xs, Fd);emit_op_implementation(G, N, [X |Xs], Fd) when record(X, attr) ->    ic_attribute_java:emit_attribute_stub_code(G, N, X, Fd),    emit_op_implementation(G, N, Xs, Fd);emit_op_implementation(G, N, [_X|Xs], Fd) ->    emit_op_implementation(G, N, Xs, Fd);emit_op_implementation(_G, _N, [], _Fd) -> ok.%%---------------------------------------%%%%  Marshal operation generation%%%%---------------------------------------emit_op_marshal(G, N, X, Fd) ->    WireOpName = ic_forms:get_id2(X),    OpName = ic_forms:get_java_id(WireOpName),    {_, ArgNames, TypeList} = extract_info(G, N, X),    {_R, ParamTypes, _} = TypeList,        PL = ic_util:mk_list(gen_marshal_par_list(G, N, X, ParamTypes, ArgNames)),        ic_codegen:emit(Fd, "    // Marshal operation for ~p\n", [OpName]),    case PL of	"" ->	    ic_codegen:emit(Fd, "    public static void _~s_marshal(~sEnvironment __env)\n", 			    [OpName, ?ICPACKAGE]),	    ic_codegen:emit(Fd, "      throws java.lang.Exception {\n\n"); 	_ ->	    ic_codegen:emit(Fd, "    public static void _~s_marshal(~sEnvironment __env, ~s)\n", 			    [OpName, ?ICPACKAGE, PL]),	    ic_codegen:emit(Fd, "      throws java.lang.Exception {\n\n")    end,    %% Message encoding    emit_op_encode(G, N, X, OpName, WireOpName, ParamTypes, ArgNames, Fd),    ic_codegen:emit(Fd, "    }\n\n").emit_op_encode(G, N, X, _OpN, WOpN, ParamTypes, ArgNames, Fd) ->    OpCallName = case ic_options:get_opt(G, scoped_op_calls) of 		     true -> 			 ic_util:to_undersc([WOpN|N]);		     false ->			 WOpN		 end,        SendParamNr = count_client_send(ArgNames),    ic_codegen:emit(Fd, "      ~sOtpOutputStream __os = __env.getOs();\n\n",		    [?ERLANGPACKAGE]),    case ic_forms:is_oneway(X) of	true ->	    %% Initiating call tuple	    ic_codegen:emit(Fd, "      // Message header assembly\n"),	    ic_codegen:emit(Fd, "      __os.reset();\n"),	    ic_codegen:emit(Fd, "      __os.write_tuple_head(2);\n"),	    ic_codegen:emit(Fd, "      __os.write_atom(\"$gen_cast\");\n\n");	false ->	    %% Initiating call tuple	    ic_codegen:emit(Fd, "      // Message header assembly\n"),	    ic_codegen:emit(Fd, "      __os.reset();\n"),	    ic_codegen:emit(Fd, "      __os.write_tuple_head(3);\n"),	    ic_codegen:emit(Fd, "      __os.write_atom(\"$gen_call\");\n\n"),	    %% Initiating call identity tuple	    ic_codegen:emit(Fd, "      // Message identity part creation\n"),	    ic_codegen:emit(Fd, "      __os.write_tuple_head(2);\n"),	    ic_codegen:emit(Fd, "      __env.write_client_pid();\n"),	    ic_codegen:emit(Fd, "      __env.write_client_ref();\n\n")    end,        %% Operation part initializations    case SendParamNr > 0 of	true ->	    ic_codegen:emit(Fd, "      // Operation attribute creation\n"),	    ic_codegen:emit(Fd, "      __os.write_tuple_head(~p);\n", [SendParamNr+1]),	    ic_codegen:emit(Fd, "      __os.write_atom(~p);\n", [OpCallName]),	    emit_op_encode_loop(G, N, X, ParamTypes, ArgNames, 1, Fd);	false -> %% No in/inout paramaters	    ic_codegen:emit(Fd, "      __os.write_atom(~p);\n", [OpCallName])    end.emit_op_encode_loop(_,_,_,_,[],_,_Fd) ->    ok;emit_op_encode_loop(G, N, X, [_Type|Types],[{out, _Arg}|Args], Counter, Fd) ->    emit_op_encode_loop(G, N, X, Types, Args, Counter, Fd);emit_op_encode_loop(G, N, X, [Type|Types], [{inout, Arg}|Args], Counter, Fd) ->    case ic_java_type:isBasicType(G, N, Type) of	true ->	    ic_codegen:emit(Fd, "      __os~s(~s.value);\n",			    [ic_java_type:marshalFun(G, N, X, Type),Arg]);	false ->	    ic_codegen:emit(Fd, "      ~s(__os, ~s.value);\n",			    [ic_java_type:marshalFun(G, N, X, Type),Arg])    end,    emit_op_encode_loop(G, N, X, Types, Args, Counter+1, Fd);emit_op_encode_loop(G, N, X, [Type|Types], [{in, Arg}|Args], Counter, Fd) ->    case ic_java_type:isBasicType(G, N, Type) of	true ->	    ic_codegen:emit(Fd, "      __os~s(~s);\n",			    [ic_java_type:marshalFun(G, N, X, Type),Arg]);	false ->	    ic_codegen:emit(Fd, "      ~s(__os, ~s);\n",			    [ic_java_type:marshalFun(G, N, X, Type),Arg])    end,    emit_op_encode_loop(G, N, X, Types, Args, Counter+1, Fd).%%-------------------------------------%%%%  UnMarshal operation generation%%%%-------------------------------------emit_op_unmarshal(G, N, X, Fd) ->    case ic_forms:is_oneway(X) of	true ->	    ok;	false ->	    OpName = ic_forms:get_java_id(X),	    {_, ArgNames, TypeList} = extract_info(G, N, X),	    {R, ParamTypes, _} = TypeList,    	    RT = ic_java_type:getParamType(G,N,R,ret),	    PL = ic_util:mk_list(gen_unmarshal_par_list(G, N, X, ParamTypes, ArgNames)),	    case PL of		"" ->		    case RT of			"void" ->			    ic_codegen:emit(Fd, "    // Unmarshal operation for ~p\n", [OpName]),			    ic_codegen:emit(Fd, "    public static void _~s_unmarshal(~sEnvironment __env)\n", 					    [OpName, ?ICPACKAGE]),			    ic_codegen:emit(Fd, "      throws java.lang.Exception {\n\n"),			    ic_codegen:emit(Fd, "        __env.getIs().read_atom();\n"),			    ic_codegen:emit(Fd, "    }\n\n");			_ ->			    ic_codegen:emit(Fd, "    // Unmarshal operation for ~p\n", [OpName]),			    ic_codegen:emit(Fd, "    public static ~s _~s_unmarshal(~sEnvironment __env)\n", 					    [RT, OpName, ?ICPACKAGE]),			    ic_codegen:emit(Fd, "      throws java.lang.Exception {\n\n"),			    ic_codegen:emit(Fd, "      // Get input stream\n"),			    ic_codegen:emit(Fd, "      ~sOtpInputStream __is = __env.getIs();\n\n",					    [?ERLANGPACKAGE]),			    emit_op_decode(G, N, X, R, RT, ParamTypes, ArgNames, Fd),			    ic_codegen:emit(Fd, "    }\n\n")		    end;		_ ->     		    ic_codegen:emit(Fd, "    // Unmarshal operation for ~p\n", [OpName]),		    ic_codegen:emit(Fd, "    public static ~s _~s_unmarshal(~sEnvironment __env, ~s)\n", 				    [RT, OpName, ?ICPACKAGE, PL]),		    ic_codegen:emit(Fd, "      throws java.lang.Exception {\n\n"),		    		    ic_codegen:emit(Fd, "      // Get input stream\n"),		    ic_codegen:emit(Fd, "      ~sOtpInputStream __is = __env.getIs();\n\n",				    [?ERLANGPACKAGE]),		    emit_op_decode(G, N, X, R, RT, ParamTypes, ArgNames, Fd),		    ic_codegen:emit(Fd, "    }\n\n")	    end    end.emit_op_decode(G, N, X, R, RT, ParamTypes, ArgNames, Fd) ->    ReceiveNr = count_client_receive(ArgNames),        case RT of 	"void" ->	    case ReceiveNr > 0 of		true ->		    		    ic_codegen:emit(Fd, "      // Extracting output values\n"),		    ic_codegen:emit(Fd, "      __is.read_tuple_head();\n"),		    ic_codegen:emit(Fd, "      __is.read_atom();\n"),		    emit_op_decode_loop(G, N, X, ParamTypes, ArgNames, 1, Fd);				false ->		    ic_codegen:emit(Fd, "      __is.read_atom();\n")

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?