2017-11-14 1 views
0

입력 방법 duktape c 함수에서 String 객체/Number 객체 인수 유형을 확인하고 String 객체/Number 객체의 값을 파싱합니다. duk_is_object()와 같은 일반적인 API가 있지만 값을 파싱하려면 올바른 객체 유형이 필요합니다.duktape duktape c 함수의 String 객체 (이와 유사하게 Number 객체)의 인수를 구문 분석하는 방법

ex: 
ecmascript code 
    var str1 = new String("duktape"); 
    var version = new Number(2.2); 
dukFunPrintArgs(str1,str2); 

duktape c function : 
dukFunPrintArgs(ctx) 
{ 
    // code to know whether the args is of type String Object/Number Object 

} 

답변

0

duktape에서 C 함수를 등록하는 방법에 대한 정보는 어디에서 찾을 수 있습니까? 그 곳에는 전달 된 매개 변수에 액세스하는 방법에 대한 세부 정보도 있습니다. 이미 당신이 시작하기 예를 찾을 수 있습니다 duktape.org의 홈페이지 : duktape의 핵심 개념

3 Add C function bindings 

To call a C function from Ecmascript code, first declare your C functions: 

/* Being an embeddable engine, Duktape doesn't provide I/O 
* bindings by default. Here's a simple one argument print() 
* function. 
*/ 
static duk_ret_t native_print(duk_context *ctx) { 
    printf("%s\n", duk_to_string(ctx, 0)); 
    return 0; /* no return value (= undefined) */ 
} 

/* Adder: add argument values. */ 
static duk_ret_t native_adder(duk_context *ctx) { 
    int i; 
    int n = duk_get_top(ctx); /* #args */ 
    double res = 0.0; 

    for (i = 0; i < n; i++) { 
    res += duk_to_number(ctx, i); 
    } 

    duk_push_number(ctx, res); 
    return 1; /* one return value */ 
} 

Register your functions e.g. into the global object: 

duk_push_c_function(ctx, native_print, 1 /*nargs*/); 
duk_put_global_string(ctx, "print"); 
duk_push_c_function(ctx, native_adder, DUK_VARARGS); 
duk_put_global_string(ctx, "adder"); 

You can then call your function from Ecmascript code: 

duk_eval_string_noresult(ctx, "print('2+3=' + adder(2, 3));"); 

하나의 스택입니다. 값 스택은 매개 변수가 저장되는 곳입니다. Getting Started 페이지에서 자세한 내용을 읽어보십시오.

관련 문제