<commit_before>#import "I<%= $cur_module.name %>.h"
//#import "api_generator/common/ruby_helpers.h"

#include "api_generator/js_helpers.h"

#import "api_generator/iphone/CMethodResult.h"
#import "api_generator/iphone/CJSConverter.h"

extern VALUE getRuby_<%= $cur_module.name %>_Module();

<%
$iphone_types = {}
$iphone_types["BOOLEAN"] = 'BOOL'
$iphone_types["INTEGER"] = 'int'
$iphone_types["FLOAT"] = 'float'
$iphone_types["STRING"] = 'NSString*'
$iphone_types["ARRAY"] = 'NSArray*'
$iphone_types["HASH"] = 'NSDictionary*'
$iphone_types["SELF_INSTANCE"] = 'id<I'+$cur_module.name+'>'

$iphone_extract_param_prefix = {}
$iphone_extract_param_prefix["BOOLEAN"] = '[((NSNumber*)'
$iphone_extract_param_prefix["INTEGER"] = '[((NSNumber*)'
$iphone_extract_param_prefix["FLOAT"] = '[((NSNumber*)'
$iphone_extract_param_prefix["STRING"] = ''
$iphone_extract_param_prefix["ARRAY"] = ''
$iphone_extract_param_prefix["HASH"] = ''
$iphone_extract_param_prefix["SELF_INSTANCE"] = ''

$iphone_extract_param_suffix = {}
$iphone_extract_param_suffix["BOOLEAN"] = ') boolValue]'
$iphone_extract_param_suffix["INTEGER"] = ') intValue]'
$iphone_extract_param_suffix["FLOAT"] = ') floatValue]'
$iphone_extract_param_suffix["STRING"] = ''
$iphone_extract_param_suffix["ARRAY"] = ''
$iphone_extract_param_suffix["HASH"] = ''
$iphone_extract_param_suffix["SELF_INSTANCE"] = ''
%>

id<I<%= $cur_module.name %>> <%= $cur_module.name %>_makeInstanceByJSObject(rho::String sid) {
    const char* szID = sid.c_str();
    id<I<%= $cur_module.name %>Factory> factory = [<%= $cur_module.name %>FactorySingleton get<%= $cur_module.name %>FactoryInstance];
    return [factory get<%= $cur_module.name %>ByID:[NSString stringWithUTF8String:szID]];
}





<% $cur_module.methods.each do |module_method|
     interface_name = "I#{$cur_module.name}"
     if module_method.access == ModuleMethod::ACCESS_STATIC
         interface_name = "I#{$cur_module.name}Singleton"
     end

%>

<% is_callback_possible = (module_method.run_in_thread != ModuleMethod::RUN_IN_THREAD_NONE) || (module_method.has_callback != ModuleMethod::CALLBACK_NONE) %>

@interface <%= $cur_module.name %>_<%= module_method.native_name %>_caller_params : NSObject

@property (assign) NSArray* params;
@property (assign) id<<%= interface_name %>> item;
@property (assign) CMethodResult* methodResult;

+(<%= $cur_module.name %>_<%= module_method.native_name %>_caller_params*) makeParams:(NSArray*)_params _item:(id<<%= interface_name %>>)_item _methodResult:(CMethodResult*)_methodResult;

@end

@implementation <%= $cur_module.name %>_<%= module_method.native_name %>_caller_params

@synthesize params,item,methodResult;

+(<%= $cur_module.name %>_<%= module_method.native_name %>_caller_params*) makeParams:(NSArray*)_params _item:(id<<%= interface_name %>>)_item _methodResult:(CMethodResult*)_methodResult {
    <%= $cur_module.name %>_<%= module_method.native_name %>_caller_params* par = [[<%= $cur_module.name %>_<%= module_method.native_name %>_caller_params alloc] init];
    par.params = _params;
    par.item = _item;
    par.methodResult = _methodResult;
    return par;
}

@end


@interface <%= $cur_module.name %>_<%= module_method.native_name %>_caller : NSObject {

}
+(<%= $cur_module.name %>_<%= module_method.native_name %>_caller*) getSharedInstance;
+(void) <%= module_method.native_name %>:(<%= $cur_module.name %>_<%= module_method.native_name %>_caller_params*)caller_params;
+(void) <%= module_method.native_name %>_in_thread:(<%= $cur_module.name %>_<%= module_method.native_name %>_caller_params*)caller_params;
+(void) <%= module_method.native_name %>_in_UI_thread:(<%= $cur_module.name %>_<%= module_method.native_name %>_caller_params*)caller_params;

@end

static <%= $cur_module.name %>_<%= module_method.native_name %>_caller* our_<%= $cur_module.name %>_<%= module_method.native_name %>_caller = nil;

@implementation <%= $cur_module.name %>_<%= module_method.native_name %>_caller

+(<%= $cur_module.name %>_<%= module_method.native_name %>_caller*) getSharedInstance {
    if (our_<%= $cur_module.name %>_<%= module_method.native_name %>_caller == nil) {
        our_<%= $cur_module.name %>_<%= module_method.native_name %>_caller = [[<%= $cur_module.name %>_<%= module_method.native_name %>_caller alloc] init];
    }
    return our_<%= $cur_module.name %>_<%= module_method.native_name %>_caller;
}

-(void) command_<%= module_method.native_name %>:(<%= $cur_module.name %>_<%= module_method.native_name %>_caller_params*)caller_params {
    NSArray* params = caller_params.params;
    id<<%= interface_name %>> objItem = caller_params.item;
    CMethodResult* methodResult = caller_params.methodResult;

    <%
    method_line = "[objItem "+module_method.native_name
    if module_method.params.size > 0
        method_line = method_line + ":(#{$iphone_types[module_method.params[0].type]})#{$iphone_extract_param_prefix[module_method.params[0].type]}[params objectAtIndex:0]#{$iphone_extract_param_suffix[module_method.params[0].type]} "
        for i in 1..(module_method.params.size-1)
            method_line = method_line + "#{module_method.params[i].name}:(#{$iphone_types[module_method.params[i].type]})#{$iphone_extract_param_prefix[module_method.params[i].type]}[params objectAtIndex:#{i}]#{$iphone_extract_param_suffix[module_method.params[i].type]} "
        end
        if (module_method.has_callback != ModuleMethod::CALLBACK_NONE) || (module_method.is_return_value  || module_method.is_factory_method)
            method_line = method_line + "methodResult:methodResult "
        end
    else
        if (module_method.has_callback != ModuleMethod::CALLBACK_NONE) || (module_method.is_return_value  || module_method.is_factory_method)
            method_line = method_line + ":methodResult "
        end
    end
    method_line = method_line + "];"
    %>
    <%= method_line %>
}


+(void) <%= module_method.native_name %>:(<%= $cur_module.name %>_<%= module_method.native_name %>_caller_params*)caller_params {
    [[<%= $cur_module.name %>_<%= module_method.native_name %>_caller getSharedInstance] command_<%= module_method.native_name %>:caller_params];
}

+(void) <%= module_method.native_name %>_in_thread:(<%= $cur_module.name %>_<%= module_method.native_name %>_caller_params*)caller_params {
    [[<%= $cur_module.name %>_<%= module_method.native_name %>_caller getSharedInstance] performSelectorInBackground:@selector(command_<%= module_method.native_name %>:) withObject:caller_params];
}

+(void) <%= module_method.native_name %>_in_UI_thread:(<%= $cur_module.name %>_<%= module_method.native_name %>_caller_params*)caller_params {
    [[<%= $cur_module.name %>_<%= module_method.native_name %>_caller getSharedInstance] performSelectorOnMainThread:@selector(command_<%= module_method.native_name %>:) withObject:caller_params waitUntilDone:NO];
}


@end


<%= "rho::String js_"+$cur_module.name+"_"+module_method.native_name+"_Obj(rho::json::CJSONArray& argv, id<#{interface_name}>objItem, const rho::String& strCallbackID, const rho::String& strJsVmID) {" %>

    CMethodResult* methodResult = [[CMethodResult alloc] init];

    NSObject* params[<%= module_method.params.size %>+1];
    BOOL method_return_result = YES;
    BOOL method_receive_callback = NO;
    int argc = argv.getSize();
    <%
     lines = prepare_rho_api_params_structure_lines(module_method.params, 'rho_api_params')
     lines.each do |line|
    %>
    <%= line %><%
     end
    %>

    <%
     factory_params = "BOOL is_factory_param[] = { "
     module_method.params.each do |method_param|
        if method_param.type == MethodParam::TYPE_SELF
            factory_params = factory_params + "YES, "
        else
            factory_params = factory_params + "NO, "
        end
     end
     factory_params = factory_params + "NO };"
    %>
    <%= factory_params %>

    int i;

    // init
    for (i = 0; i < (<%= module_method.params.size %>); i++) {
        params[i] = [CJSConverter getObjectiveCNULL];
    }

    // enumerate params
    for (int i = 0; i < (<%= module_method.params.size %>); i++) {
        if (argc > i) {
            // we have a [i] param !
            if (is_factory_param[i]) {
                params[i] = <%= $cur_module.name %>_makeInstanceByJSObject(argv.getItem(i).getString());
            }
            else {
                rho::json::CJSONEntry entry = argv.getItem(i);
                params[i] = [[CJSConverter convertFromJS:&entry rho_api_param:&(rho_api_params[i])] retain];
            }
        }<commit_msg>CommonAPI: JS: fix default params for methods on iOS<commit_after>#import "I<%= $cur_module.name %>.h"
//#import "api_generator/common/ruby_helpers.h"

#include "api_generator/js_helpers.h"

#import "api_generator/iphone/CMethodResult.h"
#import "api_generator/iphone/CJSConverter.h"

extern VALUE getRuby_<%= $cur_module.name %>_Module();

<%
$iphone_types = {}
$iphone_types["BOOLEAN"] = 'BOOL'
$iphone_types["INTEGER"] = 'int'
$iphone_types["FLOAT"] = 'float'
$iphone_types["STRING"] = 'NSString*'
$iphone_types["ARRAY"] = 'NSArray*'
$iphone_types["HASH"] = 'NSDictionary*'
$iphone_types["SELF_INSTANCE"] = 'id<I'+$cur_module.name+'>'

$iphone_extract_param_prefix = {}
$iphone_extract_param_prefix["BOOLEAN"] = '[((NSNumber*)'
$iphone_extract_param_prefix["INTEGER"] = '[((NSNumber*)'
$iphone_extract_param_prefix["FLOAT"] = '[((NSNumber*)'
$iphone_extract_param_prefix["STRING"] = ''
$iphone_extract_param_prefix["ARRAY"] = ''
$iphone_extract_param_prefix["HASH"] = ''
$iphone_extract_param_prefix["SELF_INSTANCE"] = ''

$iphone_extract_param_suffix = {}
$iphone_extract_param_suffix["BOOLEAN"] = ') boolValue]'
$iphone_extract_param_suffix["INTEGER"] = ') intValue]'
$iphone_extract_param_suffix["FLOAT"] = ') floatValue]'
$iphone_extract_param_suffix["STRING"] = ''
$iphone_extract_param_suffix["ARRAY"] = ''
$iphone_extract_param_suffix["HASH"] = ''
$iphone_extract_param_suffix["SELF_INSTANCE"] = ''
%>

id<I<%= $cur_module.name %>> <%= $cur_module.name %>_makeInstanceByJSObject(rho::String sid) {
    const char* szID = sid.c_str();
    id<I<%= $cur_module.name %>Factory> factory = [<%= $cur_module.name %>FactorySingleton get<%= $cur_module.name %>FactoryInstance];
    return [factory get<%= $cur_module.name %>ByID:[NSString stringWithUTF8String:szID]];
}





<% $cur_module.methods.each do |module_method|
     interface_name = "I#{$cur_module.name}"
     if module_method.access == ModuleMethod::ACCESS_STATIC
         interface_name = "I#{$cur_module.name}Singleton"
     end

%>

<% is_callback_possible = (module_method.run_in_thread != ModuleMethod::RUN_IN_THREAD_NONE) || (module_method.has_callback != ModuleMethod::CALLBACK_NONE) %>

@interface <%= $cur_module.name %>_<%= module_method.native_name %>_caller_params : NSObject

@property (assign) NSArray* params;
@property (assign) id<<%= interface_name %>> item;
@property (assign) CMethodResult* methodResult;

+(<%= $cur_module.name %>_<%= module_method.native_name %>_caller_params*) makeParams:(NSArray*)_params _item:(id<<%= interface_name %>>)_item _methodResult:(CMethodResult*)_methodResult;

@end

@implementation <%= $cur_module.name %>_<%= module_method.native_name %>_caller_params

@synthesize params,item,methodResult;

+(<%= $cur_module.name %>_<%= module_method.native_name %>_caller_params*) makeParams:(NSArray*)_params _item:(id<<%= interface_name %>>)_item _methodResult:(CMethodResult*)_methodResult {
    <%= $cur_module.name %>_<%= module_method.native_name %>_caller_params* par = [[<%= $cur_module.name %>_<%= module_method.native_name %>_caller_params alloc] init];
    par.params = _params;
    par.item = _item;
    par.methodResult = _methodResult;
    return par;
}

@end


@interface <%= $cur_module.name %>_<%= module_method.native_name %>_caller : NSObject {

}
+(<%= $cur_module.name %>_<%= module_method.native_name %>_caller*) getSharedInstance;
+(void) <%= module_method.native_name %>:(<%= $cur_module.name %>_<%= module_method.native_name %>_caller_params*)caller_params;
+(void) <%= module_method.native_name %>_in_thread:(<%= $cur_module.name %>_<%= module_method.native_name %>_caller_params*)caller_params;
+(void) <%= module_method.native_name %>_in_UI_thread:(<%= $cur_module.name %>_<%= module_method.native_name %>_caller_params*)caller_params;

@end

static <%= $cur_module.name %>_<%= module_method.native_name %>_caller* our_<%= $cur_module.name %>_<%= module_method.native_name %>_caller = nil;

@implementation <%= $cur_module.name %>_<%= module_method.native_name %>_caller

+(<%= $cur_module.name %>_<%= module_method.native_name %>_caller*) getSharedInstance {
    if (our_<%= $cur_module.name %>_<%= module_method.native_name %>_caller == nil) {
        our_<%= $cur_module.name %>_<%= module_method.native_name %>_caller = [[<%= $cur_module.name %>_<%= module_method.native_name %>_caller alloc] init];
    }
    return our_<%= $cur_module.name %>_<%= module_method.native_name %>_caller;
}

-(void) command_<%= module_method.native_name %>:(<%= $cur_module.name %>_<%= module_method.native_name %>_caller_params*)caller_params {
    NSArray* params = caller_params.params;
    id<<%= interface_name %>> objItem = caller_params.item;
    CMethodResult* methodResult = caller_params.methodResult;

    <%
    method_line = "[objItem "+module_method.native_name
    if module_method.params.size > 0
        method_line = method_line + ":(#{$iphone_types[module_method.params[0].type]})#{$iphone_extract_param_prefix[module_method.params[0].type]}[params objectAtIndex:0]#{$iphone_extract_param_suffix[module_method.params[0].type]} "
        for i in 1..(module_method.params.size-1)
            method_line = method_line + "#{module_method.params[i].name}:(#{$iphone_types[module_method.params[i].type]})#{$iphone_extract_param_prefix[module_method.params[i].type]}[params objectAtIndex:#{i}]#{$iphone_extract_param_suffix[module_method.params[i].type]} "
        end
        if (module_method.has_callback != ModuleMethod::CALLBACK_NONE) || (module_method.is_return_value  || module_method.is_factory_method)
            method_line = method_line + "methodResult:methodResult "
        end
    else
        if (module_method.has_callback != ModuleMethod::CALLBACK_NONE) || (module_method.is_return_value  || module_method.is_factory_method)
            method_line = method_line + ":methodResult "
        end
    end
    method_line = method_line + "];"
    %>
    <%= method_line %>
}


+(void) <%= module_method.native_name %>:(<%= $cur_module.name %>_<%= module_method.native_name %>_caller_params*)caller_params {
    [[<%= $cur_module.name %>_<%= module_method.native_name %>_caller getSharedInstance] command_<%= module_method.native_name %>:caller_params];
}

+(void) <%= module_method.native_name %>_in_thread:(<%= $cur_module.name %>_<%= module_method.native_name %>_caller_params*)caller_params {
    [[<%= $cur_module.name %>_<%= module_method.native_name %>_caller getSharedInstance] performSelectorInBackground:@selector(command_<%= module_method.native_name %>:) withObject:caller_params];
}

+(void) <%= module_method.native_name %>_in_UI_thread:(<%= $cur_module.name %>_<%= module_method.native_name %>_caller_params*)caller_params {
    [[<%= $cur_module.name %>_<%= module_method.native_name %>_caller getSharedInstance] performSelectorOnMainThread:@selector(command_<%= module_method.native_name %>:) withObject:caller_params waitUntilDone:NO];
}


@end


<%= "rho::String js_"+$cur_module.name+"_"+module_method.native_name+"_Obj(rho::json::CJSONArray& argv, id<#{interface_name}>objItem, const rho::String& strCallbackID, const rho::String& strJsVmID) {" %>

    CMethodResult* methodResult = [[CMethodResult alloc] init];

    NSObject* params[<%= module_method.params.size %>+1];
    BOOL method_return_result = YES;
    BOOL method_receive_callback = NO;
    int argc = argv.getSize();
    <%
     lines = prepare_rho_api_params_structure_lines(module_method.params, 'rho_api_params')
     lines.each do |line|
    %>
    <%= line %><%
     end
    %>

    <%
     factory_params = "BOOL is_factory_param[] = { "
     module_method.params.each do |method_param|
        if method_param.type == MethodParam::TYPE_SELF
            factory_params = factory_params + "YES, "
        else
            factory_params = factory_params + "NO, "
        end
     end
     factory_params = factory_params + "NO };"
    %>
    <%= factory_params %>

    int i;

    // init
    for (i = 0; i < (<%= module_method.params.size %>); i++) {
        params[i] = [CJSConverter getObjectiveCNULL];
    }

    <% for i in 0..(module_method.params.size-1)
          if module_method.params[i].default_value != nil
             if module_method.params[i].type == MethodParam::TYPE_STRING
                line = 'params['+i.to_s+']= @"'+module_method.params[i].default_value.to_s+'";'
                %>
                <%= line %><%
             end
             if module_method.params[i].type == MethodParam::TYPE_INT
                line = 'params['+i.to_s+']= [NSNumber numberWithInt:'+module_method.params[i].default_value.to_s+'];'
                %>
                <%= line %><%
             end
             if module_method.params[i].type == MethodParam::TYPE_DOUBLE
                line = 'params['+i.to_s+']= [NSNumber numberWithFloat:'+module_method.params[i].default_value.to_s+'];'
                %>
                <%= line %><%
             end
             if module_method.params[i].type == MethodParam::TYPE_BOOL
                line = ''
                if module_method.params[i].default_value.to_s.downcase != "false"
                     line = 'params['+i.to_s+']= [NSNumber numberWithBool:YES];'
                else
                     line = 'params['+i.to_s+']= [NSNumber numberWithBool:NO];'
                end
                %>
                <%= line %><%
             end
          end
       end
    %>

    // enumerate params
    for (int i = 0; i < (<%= module_method.params.size %>); i++) {
        if (argc > i) {
            // we have a [i] param !
            if (is_factory_param[i]) {
                params[i] = <%= $cur_module.name %>_makeInstanceByJSObject(argv.getItem(i).getString());
            }
            else {
                rho::json::CJSONEntry entry = argv.getItem(i);
                params[i] = [[CJSConverter convertFromJS:&entry rho_api_param:&(rho_api_params[i])] retain];
            }
        }
{'fraction_non_alphanumeric': 0.16532870470817965, 'fraction_numerical': 0.0013018008244738556, 'mean_word_length': 4.681664098613251, 'pattern_counts': {'":': 4, '<': 210, '<?xml version=': 0, '>': 210, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1922812', 'n_tokens_mistral': 6689, 'n_tokens_neox': 6175, 'n_words': 894}