Exemplo n.º 1
0
void SG_localsettings__reset(SG_context * pCtx, const char* psz_path)
{
    SG_jsondb* p = NULL;
    SG_string* pstr_path = NULL;

    SG_ASSERT(pCtx);
    SG_NONEMPTYCHECK_RETURN(psz_path);

    SG_ERR_CHECK(  SG_STRING__ALLOC(pCtx, &pstr_path)  );
    if ('/' == psz_path[0])
    {
        SG_ERR_CHECK(  SG_string__sprintf(pCtx, pstr_path, "%s", psz_path)  );
    }
    else
    {
        SG_ERR_CHECK(  SG_string__sprintf(pCtx, pstr_path, "%s/%s", SG_LOCALSETTING__SCOPE__MACHINE, psz_path)  );
    }

    SG_ERR_CHECK(  SG_closet__get_localsettings(pCtx, &p)  );
	SG_jsondb__remove(pCtx, p, SG_string__sz(pstr_path));
    SG_ERR_CHECK_CURRENT_DISREGARD(SG_ERR_NOT_FOUND);

fail:
	SG_JSONDB_NULLFREE(pCtx, p);
    SG_STRING_NULLFREE(pCtx, pstr_path);
}
void SG_jscore__new_context(SG_context * pCtx, JSContext ** pp_cx, JSObject ** pp_glob,
	const SG_vhash * pServerConfig)
{
	JSContext * cx = NULL;
	JSObject * glob = NULL;

	SG_ASSERT(pCtx!=NULL);
	SG_NULLARGCHECK_RETURN(pp_cx);

	if(gpJSCoreGlobalState==NULL)
		SG_ERR_THROW2_RETURN(SG_ERR_UNINITIALIZED, (pCtx, "jscore has not been initialized"));

	if (gpJSCoreGlobalState->cb)
		JS_SetContextCallback(gpJSCoreGlobalState->rt, gpJSCoreGlobalState->cb);

	cx = JS_NewContext(gpJSCoreGlobalState->rt, 8192);
	if(cx==NULL)
		SG_ERR_THROW2_RETURN(SG_ERR_MALLOCFAILED, (pCtx, "Failed to allocate new JS context"));

	(void)JS_SetContextThread(cx);
	JS_BeginRequest(cx);

	JS_SetOptions(cx, JSOPTION_VAROBJFIX);
	JS_SetVersion(cx, JSVERSION_LATEST);
	JS_SetContextPrivate(cx, pCtx);

	glob = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL);
	if(glob==NULL)
		SG_ERR_THROW2(SG_ERR_JS, (pCtx, "Failed to create JavaScript global object for new JSContext."));
	if(!JS_InitStandardClasses(cx, glob))
		SG_ERR_THROW2(SG_ERR_JS, (pCtx, "JS_InitStandardClasses() failed."));
	if (gpJSCoreGlobalState->shell_functions)
		if (!JS_DefineFunctions(cx, glob, gpJSCoreGlobalState->shell_functions))
			SG_ERR_THROW2(SG_ERR_JS, (pCtx, "Failed to install shell functions"));

    SG_jsglue__set_sg_context(pCtx, cx);

	SG_ERR_CHECK(  SG_jsglue__install_scripting_api(pCtx, cx, glob)  );
	SG_ERR_CHECK(  SG_zing_jsglue__install_scripting_api(pCtx, cx, glob)  );

	if (! gpJSCoreGlobalState->bSkipModules)
	{
		_sg_jscore__install_modules(pCtx, cx, glob, pServerConfig);
		SG_ERR_CHECK_CURRENT_DISREGARD(SG_ERR_NOTAFILE);
	}

	*pp_cx = cx;
	*pp_glob = glob;

	return;
fail:
	if (cx)
	{
		JS_EndRequest(cx);
		JS_DestroyContext(cx);
	}
}
/**
 * Create a new repo and *always* create a WD for it.
 *
 */
void vv2__do_cmd_init_new_repo(SG_context * pCtx,
							  const char * pszRepoName,
							  const char * pszFolder,
							  const char * pszStorage,
							  const char * pszHashMethod,
							  const char * psz_shared_users)
{
	SG_pathname * pPathLocalDirectory = NULL;

	SG_ERR_CHECK(  SG_PATHNAME__ALLOC__SZ(pCtx, &pPathLocalDirectory, pszFolder)  );
	SG_workingdir__find_mapping(pCtx, pPathLocalDirectory, NULL, NULL, NULL);
	SG_ERR_CHECK_CURRENT_DISREGARD(SG_ERR_NOT_A_WORKING_COPY);

	SG_ERR_CHECK(  SG_vv2__init_new_repo(pCtx,
										 pszRepoName,
										 pszFolder,
										 pszStorage,
										 pszHashMethod,
										 SG_FALSE,			// bNoWD
										 psz_shared_users,
										 SG_FALSE,
										 NULL,
										 NULL)  );

fail:
	SG_PATHNAME_NULLFREE(pCtx, pPathLocalDirectory);

	// TODO 2010/10/25 Decide which error message we should try to provide a better error message for.
	// TODO            Candidates include
	// TODO SG_ERR_INVALID_REPO_NAME
	// TODO SG_ERR_AMBIGUOUS_ID_PREFIX
	// TODO SG_ERR_MULTIPLE_HEADS_FROM_DAGNODE
	// TODO
	// TODO            But we should look and see what the lower layers are throwing now.  Some of
	// TODO            of the messages that were here are now being handled.
	return;
}
void SG_vc_hooks__execute(
        SG_context* pCtx, 
        const char* psz_js,
        SG_vhash* pvh_params,
        SG_vhash** ppvh_result
        )
{
	jsval rval;
    JSContext* cx = NULL;
    JSObject* glob = NULL;
    SG_vhash* pvh_result = NULL;
    JSObject* jso_params = NULL;

	SG_NULLARGCHECK_RETURN(psz_js);

    SG_jscore__new_runtime(pCtx, NULL, global_functions, SG_FALSE, NULL);
    SG_ERR_CHECK_CURRENT_DISREGARD(SG_ERR_ALREADY_INITIALIZED);
    SG_ERR_CHECK(  SG_jscore__new_context(pCtx, &cx, &glob, NULL)  );
	
	//This should tell the javascript engine how to report javascript errors to our jsglue.
	JS_SetErrorReporter(cx,SG_jsglue__error_reporter);

	if(!JS_EvaluateScript(cx, glob, psz_js, (uintN) strlen(psz_js), "hook_script", 1, &rval))
	{
		SG_ERR_CHECK_CURRENT;
		SG_ERR_THROW2(SG_ERR_UNSPECIFIED, (pCtx, "An error occurred evaluating hook"));
	}

    // note that we don't care about the rval from eval of the script

	{
		jsval params;
		jsval rval;
		
        if (pvh_params)
        {
            jso_params = JS_NewObject(cx, NULL, NULL, NULL);
            if(!jso_params)
                SG_ERR_THROW2(SG_ERR_MALLOCFAILED, (pCtx, "Failed to create new JS object for params to vc hook."));
            params = OBJECT_TO_JSVAL(jso_params);
            SG_ERR_CHECK(  sg_jsglue__copy_vhash_into_jsobject(pCtx, cx, pvh_params, jso_params)  );
        }
        else
        {
            params = JSVAL_NULL;
        }

		if(!JS_CallFunctionName(cx, glob, "hook", 1, &params, &rval))
		{
			SG_ERR_CHECK_CURRENT;
			SG_ERR_THROW2(SG_ERR_UNSPECIFIED, (pCtx, "An error occurred calling JS hook"));
		}

        if (JSVAL_IS_OBJECT(rval))
        {
            SG_ERR_CHECK(  sg_jsglue__jsobject_to_vhash(pCtx, cx, JSVAL_TO_OBJECT(rval), &pvh_result)  );
        }
	}

    if (ppvh_result)
    {
        *ppvh_result = pvh_result;
        pvh_result = NULL;
    }

fail:
    if (cx)
    {
		SG_ERR_IGNORE(  SG_jsglue__set_sg_context(NULL, cx)  );

		JS_EndRequest(cx);
		JS_DestroyContext(cx);

    }

    SG_VHASH_NULLFREE(pCtx, pvh_result);
}