Exemple #1
0
int main(int argc, const char *argv[]) {
    duk_context *ctx = NULL;

    ctx = duk_create_heap_default();
    if (!ctx) {
        printf("Failed to create a Duktape heap.\n");
        exit(1);
    }

    duk_push_global_object(ctx);
    duk_push_c_function(ctx, native_prime_check, 2 /*nargs*/);
    duk_put_prop_string(ctx, -2, "primeCheckNative");

    if (duk_peval_file(ctx, "prime.js") != 0) {
        printf("Error: %s\n", duk_safe_to_string(ctx, -1));
        goto finished;
    }
    duk_pop(ctx);  /* ignore result */

    duk_get_prop_string(ctx, -1, "primeTest");
    if (duk_pcall(ctx, 0) != 0) {
        printf("Error: %s\n", duk_safe_to_string(ctx, -1));
    }
    duk_pop(ctx);  /* ignore result */

 finished:
    duk_destroy_heap(ctx);

    exit(0);
}
Exemple #2
0
duk_int_t duk_peval_file_noresult(duk_context *ctx, const char *path) {
	duk_int_t rc;

	rc = duk_peval_file(ctx, path);
	duk_pop(ctx);
	return rc;
}
Exemple #3
0
DuktapeJSE::Status DuktapeJSE::analyzeSNSS(const std::string& strJsFileName,
        const std::string& strSNSSFileName,
        const std::string& strJsonParams,
        std::string& strResult)
{
    if( !FileExists(strJsFileName) )
        return Status::JSFILE_NOT_EXISTS;

    if( !FileExists(strSNSSFileName) )
        return Status::SNSS_FILE_NOT_VALID;

    if( !isJsonValid(strJsonParams))
        return Status::INVALID_JSON_PARAMS;

    duk_context* ctx = duk_create_heap_default();
    if(!ctx)
        return Status::HEAP_CREATION_ERROR;

    duk_push_global_object(ctx);
    SnssFileAPI snssFileApi(ctx, strSNSSFileName);

    if( duk_peval_file(ctx, strJsFileName.c_str()) != 0 )
    {
        LOG_MSG(ILogger::default(), LOG_LEVEL_WARN, "DuktapeJSE: %s", duk_safe_to_string(ctx, -1));
        duk_destroy_heap(ctx);
        return Status::INVALID_SOURCE_CODE;
    }
Exemple #4
0
int muduk_init_context(duk_context **duk_ctx) {
  duk_context *ctx;

  *duk_ctx=NULL;

  //ctx = duk_create_heap_default();
  ctx = duk_create_heap(muduk_alloc,
                        muduk_realloc,
                        muduk_free,
                        NULL,
                        muduk_fatal);

  // Load local script
  //
  if (duk_peval_file(ctx, glob_ctx.js_init_fn.c_str()) != 0) {
    printf("error: %s\n", duk_safe_to_string(ctx, -1));
    goto muduk_init_fail;
  }

  // register native functions?
  //
  duk_push_global_object(ctx);
  duk_push_c_function(ctx, muduk_native_info, 0);
  duk_put_prop_string(ctx, -2, "muduk_info");

  duk_push_global_object(ctx);
  duk_push_c_function(ctx, muduk_native_z, 1);
  duk_put_prop_string(ctx, -2, "muduk_z");

  duk_push_global_object(ctx);
  duk_push_c_function(ctx, muduk_native_exit, 0);
  duk_put_prop_string(ctx, -2, "muduk_exit");

  duk_push_global_object(ctx);
  duk_push_c_function(ctx, muduk_native_debug, 1);
  duk_put_prop_string(ctx, -2, "muduk_debug");

  duk_push_global_object(ctx);
  duk_push_c_function(ctx, muduk_native_tile_pair_concordance, 6);
  duk_put_prop_string(ctx, -2, "muduk_pair_conc");

  duk_push_global_object(ctx);
  duk_push_c_function(ctx, muduk_native_tile_band, 4);
  duk_put_prop_string(ctx, -2, "muduk_tile_band");

  *duk_ctx = ctx;

  return 0;
muduk_init_fail:

  printf("???\n");

  duk_destroy_heap(ctx);
  return -1;
}
Exemple #5
0
int main(int argc, const char *argv[]) {
    duk_context *ctx = NULL;
    char line[4096];
    char idx;
    int ch;

    ctx = duk_create_heap_default();
    if (!ctx) {
        printf("Failed to create a Duktape heap.\n");
        exit(1);
    }

    if (duk_peval_file(ctx, "process.js") != 0) {
        printf("Error: %s\n", duk_safe_to_string(ctx, -1));
        goto finished;
    }
    duk_pop(ctx);  /* ignore result */

    memset(line, 0, sizeof(line));
    idx = 0;
    for (;;) {
        if (idx >= sizeof(line)) {
            printf("Line too long\n");
            exit(1);
        }

        ch = fgetc(stdin);
        if (ch == 0x0a) {
            line[idx++] = '\0';

            duk_push_global_object(ctx);
            duk_get_prop_string(ctx, -1 /*index*/, "processLine");
            duk_push_string(ctx, line);
            if (duk_pcall(ctx, 1 /*nargs*/) != 0) {
                printf("Error: %s\n", duk_safe_to_string(ctx, -1));
            } else {
                printf("%s\n", duk_safe_to_string(ctx, -1));
            }
            duk_pop(ctx);  /* pop result/error */

            idx = 0;
        } else if (ch == EOF) {
            break;
        } else {
            line[idx++] = (char) ch;
        }
    }

 finished:
    duk_destroy_heap(ctx);

    exit(0);
}
Exemple #6
0
int __stdcall AddFile(duk_context *ctx, char* pth){
#pragma EXPORT
	int rv;
	char* l;
	if(ctx == 0) return -1;
	startTime = GetTickCount();
	rv = duk_peval_file(ctx, pth); //0 = success
	if(rv != 0){
		setLastString(duk_safe_to_string(ctx, -1)); //error message..
	}
	duk_pop(ctx);  /* ignore result */
	return rv;
}
Exemple #7
0
DuktapeJSE::Status DuktapeJSE::eval_from_file(const std::string& strFileName)
{
    if( !FileExists(strFileName) )
        return Status::JSFILE_NOT_EXISTS;

    duk_context* ctx = duk_create_heap_default();
    if( duk_peval_file(ctx, strFileName.c_str()) != 0)
    {
        std::cout << "Error: " << duk_safe_to_string(ctx, -1) << std::endl;
        duk_destroy_heap(ctx);
        return Status::INVALID_SOURCE_CODE;
    }
    duk_destroy_heap(ctx);
    return Status::OK;
}
Exemple #8
0
static duk_ret_t pmain (duk_context *ctx) {
	int argc; const char** argv;
	argc = duk_require_int(ctx, 0);
	argv = duk_require_pointer(ctx, 1);

	prepare_duk_env(ctx);
    duknode_push_argv(ctx, argc, argv);

    if (duk_peval_file(ctx, "node-main.js") != 0) {
        duk_error(ctx, DUK_ERR_INTERNAL_ERROR, "%s\n", duk_safe_to_string(ctx, -1));
        return -1;
    }
    duk_pop(ctx);  /* ignore result */

    return 0;
}
Exemple #9
0
void Emerobot::Run()
{
    Bot = this;


    boost::filesystem::path  program_path = GetDataDir() / "bots" / "app" / (Program + ".js");

    ctx = duk_create_heap_default();

    duk_push_global_object (ctx);

    InstallAPI();

    if ( duk_peval_file ( ctx, program_path.string().c_str() ) != 0 )
    {
        printf ( "Bot[%d/%s]: (Loading from \"%s\"): %s\n",
		 id, Program.c_str(), program_path.string().c_str(), duk_safe_to_string(ctx, -1) );

	SetTitle ( duk_safe_to_string(ctx, -1) );

        goto finished;
    }

    duk_pop (ctx);  /* ignore result */


 finished:
    ;

#if 0
    for (;;)
    {
	sleep (12);
	printf ( "Bot[%s]: Working...\n", Program.c_str() );
	duk_eval_string ( ctx, "print('Hello world!');" );
    }
#endif
}
static duk_ret_t test_func(duk_context *ctx) {
	if (ctx) {
		printf("dummy - return here\n"); fflush(stdout);
		return 0;
	}

	/* Up-to-date for Duktape 0.12.0, alphabetical order:
	 * $ cd website/api; ls *.txt
	 */

	(void) duk_alloc_raw(ctx, 0);
	(void) duk_alloc(ctx, 0);
	(void) duk_base64_decode(ctx, 0);
	(void) duk_base64_encode(ctx, 0);
	(void) duk_call_method(ctx, 0);
	(void) duk_call_prop(ctx, 0, 0);
	(void) duk_call(ctx, 0);
	(void) duk_char_code_at(ctx, 0, 0);
	(void) duk_check_stack_top(ctx, 0);
	(void) duk_check_stack(ctx, 0);
	(void) duk_check_type_mask(ctx, 0, 0);
	(void) duk_check_type(ctx, 0, 0);
	(void) duk_compact(ctx, 0);
	(void) duk_compile_file(ctx, 0, "dummy");
	(void) duk_compile_lstring_filename(ctx, 0, "dummy", 0);
	(void) duk_compile_lstring(ctx, 0, "dummy", 0);
	(void) duk_compile_string_filename(ctx, 0, "dummy");
	(void) duk_compile_string(ctx, 0, "dummy");
	(void) duk_compile(ctx, 0);
	(void) duk_concat(ctx, 0);
	(void) duk_copy(ctx, 0, 0);
	(void) duk_create_heap_default();
	(void) duk_create_heap(NULL, NULL, NULL, NULL, NULL);
	(void) duk_decode_string(ctx, 0, NULL, NULL);
	(void) duk_del_prop_index(ctx, 0, 0);
	(void) duk_del_prop_string(ctx, 0, "dummy");
	(void) duk_del_prop(ctx, 0);
	(void) duk_del_var(ctx);
	(void) duk_destroy_heap(ctx);
	(void) duk_dump_context_stderr(ctx);
	(void) duk_dump_context_stdout(ctx);
	(void) duk_dup_top(ctx);
	(void) duk_dup(ctx, 0);
	(void) duk_enum(ctx, 0, 0);
	(void) duk_equals(ctx, 0, 0);
	duk_error(ctx, 0, "dummy");  /* (void) cast won't work without variadic macros */
	(void) duk_eval_file_noresult(ctx, "dummy");
	(void) duk_eval_file(ctx, "dummy");
	(void) duk_eval_lstring_noresult(ctx, "dummy", 0);
	(void) duk_eval_lstring(ctx, "dummy", 0);
	(void) duk_eval_noresult(ctx);
	(void) duk_eval_string_noresult(ctx, "dummy");
	(void) duk_eval_string(ctx, "dummy");
	(void) duk_eval(ctx);
	(void) duk_fatal(ctx, 0, "dummy");
	(void) duk_free_raw(ctx, NULL);
	(void) duk_free(ctx, NULL);
	(void) duk_gc(ctx, 0);
	(void) duk_get_boolean(ctx, 0);
	(void) duk_get_buffer(ctx, 0, NULL);
	(void) duk_get_c_function(ctx, 0);
	(void) duk_get_context(ctx, 0);
	(void) duk_get_current_magic(ctx);
	(void) duk_get_finalizer(ctx, 0);
	(void) duk_get_global_string(ctx, 0);
	(void) duk_get_int(ctx, 0);
	(void) duk_get_length(ctx, 0);
	(void) duk_get_lstring(ctx, 0, NULL);
	(void) duk_get_magic(ctx, 0);
	(void) duk_get_memory_functions(ctx, NULL);
	(void) duk_get_number(ctx, 0);
	(void) duk_get_pointer(ctx, 0);
	(void) duk_get_prop_index(ctx, 0, 0);
	(void) duk_get_prop_string(ctx, 0, "dummy");
	(void) duk_get_prop(ctx, 0);
	(void) duk_get_prototype(ctx, 0);
	(void) duk_get_string(ctx, 0);
	(void) duk_get_top_index(ctx);
	(void) duk_get_top(ctx);
	(void) duk_get_type_mask(ctx, 0);
	(void) duk_get_type(ctx, 0);
	(void) duk_get_uint(ctx, 0);
	(void) duk_get_var(ctx);
	(void) duk_has_prop_index(ctx, 0, 0);
	(void) duk_has_prop_string(ctx, 0, "dummy");
	(void) duk_has_prop(ctx, 0);
	(void) duk_has_var(ctx);
	(void) duk_hex_decode(ctx, 0);
	(void) duk_hex_encode(ctx, 0);
	(void) duk_insert(ctx, 0);
	(void) duk_is_array(ctx, 0);
	(void) duk_is_boolean(ctx, 0);
	(void) duk_is_bound_function(ctx, 0);
	(void) duk_is_buffer(ctx, 0);
	(void) duk_is_callable(ctx, 0);
	(void) duk_is_c_function(ctx, 0);
	(void) duk_is_constructor_call(ctx);
	(void) duk_is_dynamic_buffer(ctx, 0);
	(void) duk_is_ecmascript_function(ctx, 0);
	(void) duk_is_fixed_buffer(ctx, 0);
	(void) duk_is_function(ctx, 0);
	(void) duk_is_nan(ctx, 0);
	(void) duk_is_null_or_undefined(ctx, 0);
	(void) duk_is_null(ctx, 0);
	(void) duk_is_number(ctx, 0);
	(void) duk_is_object_coercible(ctx, 0);
	(void) duk_is_object(ctx, 0);
	(void) duk_is_pointer(ctx, 0);
	(void) duk_is_primitive(ctx, 0);
	(void) duk_is_strict_call(ctx);
	(void) duk_is_string(ctx, 0);
	(void) duk_is_thread(ctx, 0);
	(void) duk_is_undefined(ctx, 0);
	(void) duk_is_valid_index(ctx, 0);
	(void) duk_join(ctx, 0);
	(void) duk_json_decode(ctx, 0);
	(void) duk_json_encode(ctx, 0);
	(void) duk_map_string(ctx, 0, NULL, NULL);
	(void) duk_new(ctx, 0);
	(void) duk_next(ctx, 0, 0);
	(void) duk_normalize_index(ctx, 0);
	(void) duk_pcall_method(ctx, 0);
	(void) duk_pcall_prop(ctx, 0, 0);
	(void) duk_pcall(ctx, 0);
	(void) duk_pcompile_file(ctx, 0, "dummy");
	(void) duk_pcompile_lstring_filename(ctx, 0, "dummy", 0);
	(void) duk_pcompile_lstring(ctx, 0, "dummy", 0);
	(void) duk_pcompile_string_filename(ctx, 0, "dummy");
	(void) duk_pcompile_string(ctx, 0, "dummy");
	(void) duk_pcompile(ctx, 0);
	(void) duk_peval_file_noresult(ctx, "dummy");
	(void) duk_peval_file(ctx, "dummy");
	(void) duk_peval_lstring_noresult(ctx, "dummy", 0);
	(void) duk_peval_lstring(ctx, "dummy", 0);
	(void) duk_peval_noresult(ctx);
	(void) duk_peval_string_noresult(ctx, "dummy");
	(void) duk_peval_string(ctx, "dummy");
	(void) duk_peval(ctx);
	(void) duk_pop_2(ctx);
	(void) duk_pop_3(ctx);
	(void) duk_pop_n(ctx, 0);
	(void) duk_pop(ctx);
	(void) duk_push_array(ctx);
	(void) duk_push_boolean(ctx, 0);
	(void) duk_push_buffer(ctx, 0, 0);
	(void) duk_push_c_function(ctx, NULL, 0);
	(void) duk_push_context_dump(ctx);
	(void) duk_push_current_function(ctx);
	(void) duk_push_current_thread(ctx);
	(void) duk_push_dynamic_buffer(ctx, 0);
	(void) duk_push_error_object(ctx, 0, "dummy");
	(void) duk_push_false(ctx);
	(void) duk_push_fixed_buffer(ctx, 0);
	(void) duk_push_global_object(ctx);
	(void) duk_push_global_stash(ctx);
	(void) duk_push_heap_stash(ctx);
	(void) duk_push_int(ctx, 0);
	(void) duk_push_lstring(ctx, "dummy", 0);
	(void) duk_push_nan(ctx);
	(void) duk_push_null(ctx);
	(void) duk_push_number(ctx, 0.0);
	(void) duk_push_object(ctx);
	(void) duk_push_pointer(ctx, NULL);
	(void) duk_push_sprintf(ctx, "dummy");
	(void) duk_push_string_file(ctx, "dummy");
	(void) duk_push_string(ctx, "dummy");
	(void) duk_push_this(ctx);
	(void) duk_push_thread_new_globalenv(ctx);
	(void) duk_push_thread_stash(ctx, NULL);
	(void) duk_push_thread(ctx);
	(void) duk_push_true(ctx);
	(void) duk_push_uint(ctx, 0);
	(void) duk_push_undefined(ctx);
	(void) duk_push_vsprintf(ctx, "dummy", NULL);
	(void) duk_put_function_list(ctx, 0, NULL);
	(void) duk_put_number_list(ctx, 0, NULL);
	(void) duk_put_prop_index(ctx, 0, 0);
	(void) duk_put_prop_string(ctx, 0, "dummy");
	(void) duk_put_prop(ctx, 0);
	(void) duk_put_var(ctx);
	(void) duk_realloc_raw(ctx, NULL, 0);
	(void) duk_realloc(ctx, NULL, 0);
	(void) duk_remove(ctx, 0);
	(void) duk_replace(ctx, 0);
	(void) duk_require_boolean(ctx, 0);
	(void) duk_require_buffer(ctx, 0, NULL);
	(void) duk_require_c_function(ctx, 0);
	(void) duk_require_context(ctx, 0);
	(void) duk_require_int(ctx, 0);
	(void) duk_require_lstring(ctx, 0, NULL);
	(void) duk_require_normalize_index(ctx, 0);
	(void) duk_require_null(ctx, 0);
	(void) duk_require_number(ctx, 0);
	(void) duk_require_object_coercible(ctx, 0);
	(void) duk_require_pointer(ctx, 0);
	(void) duk_require_stack_top(ctx, 0);
	(void) duk_require_stack(ctx, 0);
	(void) duk_require_string(ctx, 0);
	(void) duk_require_top_index(ctx);
	(void) duk_require_type_mask(ctx, 0, 0);
	(void) duk_require_uint(ctx, 0);
	(void) duk_require_undefined(ctx, 0);
	(void) duk_require_valid_index(ctx, 0);
	(void) duk_resize_buffer(ctx, 0, 0);
	(void) duk_safe_call(ctx, NULL, 0, 0);
	(void) duk_safe_to_lstring(ctx, 0, NULL);
	(void) duk_safe_to_string(ctx, 0);
	(void) duk_set_finalizer(ctx, 0);
	(void) duk_set_global_object(ctx);
	(void) duk_set_magic(ctx, 0, 0);
	(void) duk_set_prototype(ctx, 0);
	(void) duk_set_top(ctx, 0);
	(void) duk_strict_equals(ctx, 0, 0);
	(void) duk_substring(ctx, 0, 0, 0);
	(void) duk_swap_top(ctx, 0);
	(void) duk_swap(ctx, 0, 0);
	(void) duk_throw(ctx);
	(void) duk_to_boolean(ctx, 0);
	(void) duk_to_buffer(ctx, 0, NULL);
	(void) duk_to_defaultvalue(ctx, 0, 0);
	(void) duk_to_dynamic_buffer(ctx, 0, NULL);
	(void) duk_to_fixed_buffer(ctx, 0, NULL);
	(void) duk_to_int32(ctx, 0);
	(void) duk_to_int(ctx, 0);
	(void) duk_to_lstring(ctx, 0, NULL);
	(void) duk_to_null(ctx, 0);
	(void) duk_to_number(ctx, 0);
	(void) duk_to_object(ctx, 0);
	(void) duk_to_pointer(ctx, 0);
	(void) duk_to_primitive(ctx, 0, 0);
	(void) duk_to_string(ctx, 0);
	(void) duk_to_uint16(ctx, 0);
	(void) duk_to_uint32(ctx, 0);
	(void) duk_to_uint(ctx, 0);
	(void) duk_to_undefined(ctx, 0);
	(void) duk_trim(ctx, 0);
	(void) duk_xcopy_top(ctx, NULL, 0);
	(void) duk_xmove_top(ctx, NULL, 0);

	printf("never here\n"); fflush(stdout);
	return 0;
}
Exemple #11
0
int main(int argc, char *argv[]) {
	/* initialize js environment */
	duk_context *ctx = duk_create_heap_default();
	
	/* add native functions to js environment */
	duk_push_global_object(ctx);
	/* 'read' */
	duk_push_c_function(ctx, js_read_file, 1);
	duk_put_prop_string(ctx, -2, "read");
#ifdef USE_PROMPT
	/* 'prompt' */
	duk_push_c_function(ctx, js_prompt, 1);
	duk_put_prop_string(ctx, -2, "prompt");
#endif
	/* 'get' */
	duk_push_c_function(ctx, js_level_get, 2);
	duk_put_prop_string(ctx, -2, "get");
	/* 'set' */
	duk_push_c_function(ctx, js_level_set, 3);
	duk_put_prop_string(ctx, -2, "set");
	/* 'register' */
	duk_push_c_function(ctx, js_register_block, 3);
	duk_put_prop_string(ctx, -2, "register");
	/* 'load_file' */
	duk_push_c_function(ctx, js_load_file, 1);
	duk_put_prop_string(ctx, -2, "load_file");
	/* 'fill' */
	duk_push_c_function(ctx, js_algo_fill, 1);
	duk_put_prop_string(ctx, -2, "fill");
	/* 'findall' */
	duk_push_c_function(ctx, js_algo_findall, 1);
	duk_put_prop_string(ctx, -2, "findall");
	/* 'neighbors_of' */
	duk_push_c_function(ctx, js_algo_neighbors_of, 3);
	duk_put_prop_string(ctx, -2, "neighbors_of");
	/* 'distance' */
	duk_push_c_function(ctx, js_algo_raycast, 4);
	duk_put_prop_string(ctx, -2, "raycast");
	/* pop global object */
	duk_pop(ctx);
	
	/* try to load some basic functionality */
	if (duk_peval_file(ctx, "base/base.js") != 0) {
		//printf("Failed to load 'base/base.js': %s\n", duk_safe_to_string(ctx, -1));
		
		/* When failing to load base/base.js, se simple, default module loader. */
		duk_eval_string_noresult(ctx, "Duktape.modSearch = function(id) { return read(id); };");
	}
	duk_pop(ctx);
	
	if (argc > 1) {
		/* run garbage collector (twice as told by the docs) */
		duk_gc(ctx, 0);
		duk_gc(ctx, 0);
		
		/* where to save the png */
		char *export_file = "generated_map";
		int generate_num = 1;
		int save_png_disabled = 0;

		/* parse command line flags */
		for (int i = 0; i < argc; ++i) {
			if (!strcmp(argv[i], "--width") || !strcmp(argv[i], "-w")) {
				if (i + 1 < argc)
					js_level_width = atoi(argv[i + 1]);
			} else if (!strcmp(argv[i], "--height") || !strcmp(argv[i], "-h")) {
				if (i + 1 < argc)
					js_level_height = atoi(argv[i + 1]);
			} else if (!strcmp(argv[i], "-o") || !strcmp(argv[i], "--output")) {
				if (i + 1 < argc)
					export_file = argv[i + 1];
			} else if (!strcmp(argv[i], "-c") || !strcmp(argv[i], "--count")) {
				if (i + 1 < argc)
					generate_num = atoi(argv[i + 1]);
			} else if (!strcmp(argv[i], "-n") || !strcmp(argv[i], "--no-export")) {
				save_png_disabled = 1;
			}
		}
		
		/* reserve enough memory for level */
		js_level = malloc(sizeof(int) * js_level_width * js_level_height);
		blocks = malloc(sizeof(struct color) * blocks_max_len);
		
		/* set all zeroes, maybe change this use to calloc */
		memset(js_level, 0, sizeof(int) * js_level_width * js_level_height);

		/* declare & assign global variable Width & Height */
		duk_push_int(ctx, js_level_width);
		duk_put_global_string(ctx, "Width");
		duk_push_int(ctx, js_level_height);
		duk_put_global_string(ctx, "Height");
		
		/* can generate multiple maps */
		for (int i = 0; i < generate_num; ++i) {
			if (duk_peval_file(ctx, argv[1]) != 0) {
				printf("('%s' #%d) %s\n", argv[1], i, duk_safe_to_string(ctx, -1));
			}
			duk_pop(ctx);
			
			/* export the level with a special name */
			char export_file_num[256];
			if (generate_num > 1) {
				sprintf(export_file_num, "%s_%d.png", export_file, i);
			} else {
				sprintf(export_file_num, "%s.png", export_file);
			}
			/* export the level as png */
			if (!save_png_disabled)
				export_level(export_file_num);
		}

		/* free all memory */ 
		free(js_level);
		free(blocks);
	} else {
		/* tell the user how to use this */
		print_usage(argv[0]);
	}

	/* clean up */
	duk_destroy_heap(ctx);
	return 0;
}
//duk_int_t duk_peval_file(duk_context *ctx, const char *path);
duk_int_t aperl_duk_peval_file(duk_context *ctx, const char *path) {
	duk_int_t ret = duk_peval_file(ctx, path);
	return ret;
}
int8_t sendSms(duk_context *JsContext, InputParameters_t *Parameters, char *Input[])
{
    extern const char const *ErrorMessages[];
    extern HttpHandle_t *Handle;
    extern Output_t OutputMessages;
    int8_t RetCode = EXIT_SUCCESS;
    
    char *GatewayLocation;
    
    if (Parameters->GatewayLocation)
    {
        uint8_t GatewayLocationLength = strlen(Input[Parameters->GatewayLocation] );
        
        if ( Input[Parameters->GatewayLocation][GatewayLocationLength - 1] == '/')
        {
            Input[Parameters->GatewayLocation][GatewayLocationLength - 1] = '\0';
            GatewayLocationLength--;
        }
        
        // 10 = '/' + ".gateway" + '\0'
        GatewayLocation = malloc(GatewayLocationLength + strlen(Input[Parameters->Gateway] ) + 10);
        
        if (!GatewayLocation)
        {
            setErrorMsg(ErrorMessages[MEMORY], true);
            return NO_MEMORY;
        }
        
        sprintf(GatewayLocation, "%s/%s.gateway",Input[Parameters->GatewayLocation], Input[Parameters->Gateway] );
    }
    else
    {
        // 27 = "/usr/share/esmska/" + ".gateway" + '\0'
        GatewayLocation = malloc(strlen(Input[Parameters->Gateway] ) + 27);
        
        if (!GatewayLocation)
        {
            setErrorMsg(ErrorMessages[MEMORY], true);
            return NO_MEMORY;
        }
        
        sprintf(GatewayLocation, "/usr/share/esmska/%s.gateway", Input[Parameters->Gateway] );
    }
    
    if (duk_peval_file(JsContext, GatewayLocation) )
    {
        duk_get_prop_string(JsContext, -1, "message");
        
        setErrorMsg(ErrorMessages[MODULE_PROCESSING], false);
        setErrorMsg(Input[Parameters->Gateway], false);
        setErrorMsg(duk_get_string(JsContext, -1), true);
        
        free(GatewayLocation);
        return MODULE_ERROR;
    }
    
    free(GatewayLocation);
    duk_pop(JsContext); // Zahozeni vysledku (undefined)
    
    // Zpristupneni potrebnych udaju jako username, passwd, cislo, text zpravy
    duk_push_global_object(JsContext);
    
    if (Parameters->User) duk_push_string(JsContext, Input[Parameters->User] );
    else duk_push_string(JsContext, "");
    duk_put_prop_string(JsContext, -2, "LOGIN");
    
    if (Parameters->Password) duk_push_string(JsContext, Input[Parameters->Password] );
    else duk_push_string(JsContext, "");
    duk_put_prop_string(JsContext, -2, "PASSWORD");
    
    if (Parameters->SenderNumber) duk_push_string(JsContext, Input[Parameters->SenderNumber] );
    else duk_push_string(JsContext, "");
    duk_put_prop_string(JsContext, -2, "SENDERNUMBER");
    
    duk_push_string(JsContext, Input[Parameters->Number] );
    duk_put_prop_string(JsContext, -2, "NUMBER");
    
    duk_push_string(JsContext, Input[Parameters->Text] );
    duk_put_prop_string(JsContext, -2, "MESSAGE");
    
    if (duk_peval_string(JsContext, "RECEIPT = (getFeatures().indexOf('RECEIPT') > -1 ? true : false)") )
    {
        duk_get_prop_string(JsContext, -1, "message");
        
        setErrorMsg(ErrorMessages[RECEIPT_SETTING], false);
        setErrorMsg(duk_get_string(JsContext, -1), true);
        
        return MODULE_ERROR;
    }
    
    duk_pop(JsContext);
    
    // Zpristupneni potrebnych metod, promennych a samotny objekt EXEC
    duk_push_object(JsContext);
    duk_push_c_function(JsContext, get, 2);
    duk_put_prop_string(JsContext, -2, "getURL");
    duk_push_c_function(JsContext, post, 3);
    duk_put_prop_string(JsContext, -2, "postURL");
    duk_push_c_function(JsContext, setProblem, 2);
    duk_put_prop_string(JsContext, -2, "setProblem");
    duk_push_c_function(JsContext, setSupplementalMsg, 1);
    duk_put_prop_string(JsContext, -2, "setSupplementalMessage");
    duk_push_c_function(JsContext, stall, 1);
    duk_put_prop_string(JsContext, -2, "sleep");
    duk_push_c_function(JsContext, setReferer, 1);
    duk_put_prop_string(JsContext, -2, "setReferer");
    
    duk_push_string(JsContext, "Free SMS remaining: ");
    duk_put_prop_string(JsContext, -2, "INFO_FREE_SMS_REMAINING");
    duk_push_string(JsContext, "The gateway doesn't provide any information about successful sending. The message might be and might not be delivered.");
    duk_put_prop_string(JsContext, -2, "INFO_STATUS_NOT_PROVIDED");
    duk_push_string(JsContext, "Remaining credit: ");
    duk_put_prop_string(JsContext, -2, "INFO_CREDIT_REMAINING");
    
    duk_put_prop_string(JsContext, -2, "EXEC");
    
    // Vytvoreni CURL contextu
    if ( !(Handle = httpInit() ) )
    {
        setErrorMsg(ErrorMessages[NO_CURL_CONTEXT], true);
        
        return CURL_ERROR;
    }
    
    // Volani funkce send()
    duk_get_prop_string(JsContext, -1, "send");
    if (duk_pcall(JsContext, 0) )
    {
        if (duk_is_number(JsContext, -1) )
        {
            uint8_t ErrCode = duk_get_number(JsContext, -1);
            
            if (ErrCode < 90)
            {
                setErrorMsg(ErrorMessages[GENERIC], false);
                setErrorMsg(curl_easy_strerror(ErrCode), true);
                
                RetCode = CURL_ERROR;
            }
            else
            {
                OutputMessages.MessagesArray[OutputMessages.NextFree].MsgType = ERR;
                OutputMessages.MessagesArray[OutputMessages.NextFree].IsEndOfMsg = 1;
                
                switch (ErrCode)
                {
                    case NO_MEMORY:
                        OutputMessages.MessagesArray[OutputMessages.NextFree++].Msg = ErrorMessages[MEMORY];
                        RetCode = INTERNAL_ERROR;
                    break;
                    
                    case NO_POST_DATA:
                        OutputMessages.MessagesArray[OutputMessages.NextFree++].Msg = ErrorMessages[POST_DATA];
                        RetCode = MODULE_ERROR;
                    break;
                    
                    case SET_PROBLEM_NO_MSG:
                        OutputMessages.MessagesArray[OutputMessages.NextFree++].Msg = ErrorMessages[NO_EXPLAINING];
                        RetCode = MODULE_ERROR;
                    break;
                    
                    case SLEEP_NOT_NUMBER:
                        OutputMessages.MessagesArray[OutputMessages.NextFree++].Msg = ErrorMessages[SLEEP_NUMBER];
                        RetCode = MODULE_ERROR;
                    break;
                    
                    case ODD_URL_PARAMETERS:
                        OutputMessages.MessagesArray[OutputMessages.NextFree++].Msg = ErrorMessages[URL_PARAMETERS];
                        RetCode = MODULE_ERROR;
                    break;
                    
                    case ODD_POST_PARAMETERS:
                        OutputMessages.MessagesArray[OutputMessages.NextFree++].Msg = ErrorMessages[POST_PARAMETERS];
                        RetCode = MODULE_ERROR;
                    break;
                }
            }
        }
        else
        {
            setErrorMsg(duk_safe_to_string(JsContext, -1), true);
            
            RetCode = DUKTAPE_ERROR;
        }
    }
    else
    {
        bool SendRetVal;
        
        if (duk_is_boolean(JsContext, -1) ) SendRetVal = duk_get_boolean(JsContext, -1);
        else
        {
            setErrorMsg(ErrorMessages[NOT_BOOL], true);
            
            SendRetVal = (bool) ( (int8_t) duk_to_number(JsContext, -1) );
        }
            
        if (!SendRetVal) RetCode = GATEWAY_ERROR;
    }
    
    httpCleanUp(Handle);
    
    return RetCode;
}