예제 #1
0
PJS_Function *
PJS_DefineFunction(PJS_Context *inContext, const char *functionName, SV *perlCallback) {
    PJS_Function *function;
    JSContext    *js_context = inContext->cx;
    SV *sv;
    
    if (PJS_GetFunctionByName(inContext, functionName) != NULL) {
        warn("Function named '%s' is already defined in the context");
        return NULL;
    }
    
    if ((function = PJS_CreateFunction(functionName, perlCallback)) == NULL) {
        return NULL;
    }
    
    /* Add the function to the javascript context */
    if (JS_DefineFunction(js_context, JS_GetGlobalObject(js_context), functionName, PJS_invoke_perl_function, 0, 0) == JS_FALSE) {
        warn("Failed to define function");
        PJS_DestroyFunction(function);
        return NULL;
    }

    sv = newSV(0);
	sv_setref_pv(sv, "JavaScript::PerlFunction", (void*) function);
	
    if (functionName != NULL) {
        SvREFCNT_inc(sv);
        hv_store(inContext->function_by_name, functionName, strlen(functionName), sv, 0);
    }
    
    return function;
}
예제 #2
0
/*
  Free memory occupied by PJS_Class structure
*/
void PJS_free_class(PJS_Class *pcls) {
    PJS_Function *method;
    PJS_Property *property;
    if (pcls == NULL) {
        return;
    }

    if (pcls->cons != NULL) {
        SvREFCNT_dec(pcls->cons);
    }

    if (pcls->pkg != NULL) {
        Safefree(pcls->pkg);
    }
    
    method = pcls->methods;
    while (method != NULL) {
        PJS_Function *next = method->_next;
        PJS_DestroyFunction(method);
        method = next;
    }
    PJS_free_JSFunctionSpec(pcls->fs);
    PJS_free_JSFunctionSpec(pcls->static_fs);
    
    property = pcls->properties;
    while (property != NULL) {
        PJS_Property *next = property->_next;
        PJS_free_property(property);
        property = next;
    }
    PJS_free_JSPropertySpec(pcls->ps);
    PJS_free_JSPropertySpec(pcls->static_ps);
    
    /* Seems like SM handles this part for us */
/*    if (pcls->flags & PJS_FREE_JSCLASS) {
        Safefree(pcls->clasp->name);
        Safefree(pcls->clasp);
    }  */
    
    Safefree(pcls);
}