// 回填 ref/out 参数
 // 扩展方法参数索引需要偏移
 protected void _WriteBackParametersByRef(bool isExtension, List <ParameterInfo> parametersByRef)
 {
     for (var i = 0; i < parametersByRef.Count; i++)
     {
         var parameter = parametersByRef[i];
         var position  = isExtension ? parameter.Position - 1 : parameter.Position;
         var argname   = $"arg{position}";
         var pusher    = cg.AppendValuePusher(parameter.ParameterType, argname);
         cg.cs.AppendLine("var out{0} = {1};", position, pusher);
         cg.cs.AppendLine("if (JSApi.JS_IsException(out{0}))", position);
         cg.cs.AppendLine("{");
         cg.cs.AddTabLevel();
         // for (var j = 0; j < i; j++)
         // {
         //     cg.cs.AppendLine("JSApi.JS_FreeValue(ctx, out{0});", j);
         // }
         cg.cs.AppendLine("return out{0};", position);
         cg.cs.DecTabLevel();
         cg.cs.AppendLine("}");
         cg.cs.AppendLine("JSApi.JS_SetPropertyStr(ctx, argv[{0}], \"target\", out{1});", position, position);
     }
 }
Exemple #2
0
        // 回填 ref/out 参数
        // 扩展方法参数索引需要偏移
        protected string _WriteBackParametersByRef(bool isExtension, List <ParameterInfo> parametersByRef, string retJsVar)
        {
            var retVar = "mult_ret";

            cg.cs.AppendLine("var context = ScriptEngine.GetContext(ctx);");
            cg.cs.AppendLine("var {0} = JSApi.JS_NewObject(ctx);", retVar);

            if (!string.IsNullOrEmpty(retJsVar))
            {
                var retJsVarName = BindingManager.GetTSVariable("return");
                cg.cs.AppendLine("JSApi.JS_SetProperty(ctx, {0}, context.GetAtom(\"{1}\"), {2});", retVar, retJsVarName, retJsVar);
            }

            for (var i = 0; i < parametersByRef.Count; i++)
            {
                var parameter = parametersByRef[i];
                var pname     = BindingManager.GetTSVariable(parameter);
                var position  = isExtension ? parameter.Position - 1 : parameter.Position;
                var argname   = $"arg{position}";
                var pusher    = cg.AppendValuePusher(parameter.ParameterType, argname);

                cg.cs.AppendLine("var out{0} = {1};", i, pusher);
                cg.cs.AppendLine("if (JSApi.JS_IsException(out{0}))", i);
                using (cg.cs.Block())
                {
                    for (var j = 0; j < i; j++)
                    {
                        cg.cs.AppendLine("JSApi.JS_FreeValue(ctx, out{0});", j);
                    }
                    cg.cs.AppendLine("JSApi.JS_FreeValue(ctx, {0});", retVar);
                    cg.cs.AppendLine("return out{0};", i);
                }
                cg.cs.AppendLine("JSApi.JS_SetProperty(ctx, {0}, context.GetAtom(\"{1}\"), out{2});", retVar, pname, i);
            }
            return(retVar);
        }