2016-04-13 14 views
0

Yerel bir C-duktape modSearch'i uygulamaya çalışıyorum ve sıkıştım. DUKtape belgelerini okudum ve https://github.com/svaarala/duktape/issues/194'a baktım ama yine de çalışamıyorum. duktape modSearch. Yerel uygulama C

ben modSearch uygulamak kullanıyorum basit bir test yarattı, aşağıda detayları aşağıdaki gibidir:
  • bir kare işlevi uygulayan bir basit javascript var. Ben buna testR.js diyoruz:

    exports.area = işlev (r) { dönüş r * r; };

basit fonksiyonu üzerinde kullandığı

-A dosya adı verilir usetestR.js: Şöyledir olarak duktape ile Şimdi

function main(){ 
// const square = require('./testR.js'); --> use this line for nodeJS 
const square = require('testR.js'); 
var ts = square.area(8); 

    // console.log(ts); -> used this line for nodeJS 
    print(ts); 
} 

main(); 

, ben C fonksiyon modSearch hayata başladı:

/* Declaration */ 
void modSearch_register(duk_context *ctx) { 
    duk_get_global_string(ctx, "Duktape"); 
    duk_push_c_function(ctx, mod_search, 4 /*nargs*/); 
    duk_put_prop_string(ctx, -2, "modSearch"); 
    duk_pop(ctx); 
} 

mod_search

#include <stdio.h> 
#include <string.h> 
#include "duktape.h" 

duk_ret_t mod_search(duk_context *ctx) { 
/* Nargs was given as 4 and we get the following stack arguments: 
* index 0: id 
* index 1: require 
* index 2: exports 
* index 3: module 
*/ 

int  rc; 

// Get ID 
char *id  = duk_require_string(ctx, 0); 

printf("ID => %s \n", id); 
rc = strcmp(id, "testR.js"); 
if(rc == 0) 
{ 
    printf("Module found, loading... \n"); 

    // Read File 
    duk_push_object(ctx); 
    duk_put_global_string(ctx, "exports"); 
    if(duk_peval_file(ctx,"testR.js")!= 0) 
     printf("Problem !!! \n"); 
    else{ 
     printf("Pass !!! \n"); 
     return 1; 
    } 
    return -1; 
} 

Kodu çalıştırdığımda bu benim elimde ne var:

ID => testR.js 
Module found, loading... 
Pass !!! 
TypeError: undefined not callable 
    duk_js_call.c:776 
    main usetestR.js:3 
    global usetestR.js:8 preventsyield 
error in executing file usetestR.js 

Bana yardım edip sorunun nerede olduğuna işaret edebilir misiniz? Teşekkür

cevap

1

Üzgünüm, geleceğe :) Aşağıda

içinde diğerlerine yardımcı olabilir Belki örnek program çalıştırma

duk_ret_t mod_search(duk_context *ctx) { 
    /* Nargs was given as 4 and we get the following stack arguments: 
    * index 0: id 
    * index 1: require 
    * index 2: exports 
    * index 3: module 
    */ 
char *src = NULL; 
FILE *f = NULL; 
const char *filename = "/home/testR.js"; 

int rc, len; 

// Pull Arguments 
char *id  = duk_require_string(ctx, 0); 

printf("ID => %s \n", id); 

rc = strcmp(id, "testR.js"); 
if(rc == 0) 
{ 
    printf("Module found, loading... \n"); 
    // Read File and calculate its size (as DUKtape examples) 
    f = fopen(filename, "rb"); 
    fseek(f, 0, SEEK_END); 
    len = (int) ftell(f); 

    // Rewind 
    fseek(f, 0, SEEK_SET); 

    src = malloc(len); 
    fread(src, 1, len,f); 
    fclose(f); 
    duk_push_lstring(ctx, src, len); 
    free(src); 
    return 1; 
    } 

    // Error 
    return -1; 
} 

çalışır modSearch olduğunu ... bunu anladım, gösterir temelleri ile

ID => testR.js 
Module found, loading... 
64 

o modSearch uygulamak için daha sofistike yollar yapmak mümkün olurdu anladı.