v8::Local<v8::Value> ScriptRunner::runCompiledScript(v8::Handle<v8::Script> script, ScriptExecutionContext* context) { if (script.IsEmpty()) return v8::Local<v8::Value>(); V8GCController::checkMemoryUsage(); if (V8RecursionScope::recursionLevel() >= kMaxRecursionDepth) return handleMaxRecursionDepthExceeded(); if (handleOutOfMemory()) return v8::Local<v8::Value>(); // Run the script and keep track of the current recursion depth. v8::Local<v8::Value> result; { V8RecursionScope recursionScope(context); result = script->Run(); } if (handleOutOfMemory()) ASSERT(result.IsEmpty()); if (result.IsEmpty()) return v8::Local<v8::Value>(); crashIfV8IsDead(); return result; }
v8::Local<v8::Value> V8Proxy::runScriptInternal(v8::Handle<v8::Script> script, bool isInlineCode) #endif { if (script.IsEmpty()) return notHandledByInterceptor(); V8GCController::checkMemoryUsage(); // Compute the source string and prevent against infinite recursion. if (m_recursion >= kMaxRecursionDepth) { v8::Local<v8::String> code = v8ExternalString("throw RangeError('Recursion too deep')"); // FIXME: Ideally, we should be able to re-use the origin of the // script passed to us as the argument instead of using an empty string // and 0 baseLine. script = compileScript(code, "", 0); } if (handleOutOfMemory()) ASSERT(script.IsEmpty()); if (script.IsEmpty()) return notHandledByInterceptor(); // Save the previous value of the inlineCode flag and update the flag for // the duration of the script invocation. bool previousInlineCode = inlineCode(); setInlineCode(isInlineCode); // Run the script and keep track of the current recursion depth. v8::Local<v8::Value> result; { V8ConsoleMessage::Scope scope; // See comment in V8Proxy::callFunction. m_frame->keepAlive(); m_recursion++; result = script->Run(); m_recursion--; } // Release the storage mutex if applicable. releaseStorageMutex(); if (handleOutOfMemory()) ASSERT(result.IsEmpty()); // Handle V8 internal error situation (Out-of-memory). if (result.IsEmpty()) return notHandledByInterceptor(); // Restore inlineCode flag. setInlineCode(previousInlineCode); if (v8::V8::IsDead()) handleFatalErrorInV8(); return result; }
int XSetFontPath(Display* display, char** directories, int ndirs) { SET_X_SERVER_REQUEST(display, X_SetFontPath); char* path; size_t i; while (fontSearchPaths->length > 0) { // Clear the array and free the data free(removeArray(fontSearchPaths, 0, False)); } if (directories == NULL || ndirs == 0) { // Reset the search path to the default for the compiled platform ndirs = ARRAY_LENGTH(DEFAULT_FONT_SEARCH_PATHS); directories = (char **) DEFAULT_FONT_SEARCH_PATHS; } for (i = 0; i < ndirs; i++) { if (checkFontPath(directories[i])) { path = strdup(directories[i]); if (path == NULL) { handleOutOfMemory(0, display, 0, 0); } else { insertArray(fontSearchPaths, path); } } } return updateFontCache() ? 1 : 0; }
v8::Local<v8::Value> WorkerContextExecutionProxy::runScript(v8::Handle<v8::Script> script) { if (script.IsEmpty()) return v8::Local<v8::Value>(); // Compute the source string and prevent against infinite recursion. if (V8RecursionScope::recursionLevel() >= kMaxRecursionDepth) { v8::Local<v8::String> code = v8ExternalString("throw RangeError('Recursion too deep')"); script = ScriptSourceCode::compileScript(code, "", TextPosition::minimumPosition()); } if (handleOutOfMemory()) ASSERT(script.IsEmpty()); if (script.IsEmpty()) return v8::Local<v8::Value>(); // Run the script and keep track of the current recursion depth. v8::Local<v8::Value> result; { V8RecursionScope recursionScope(m_workerContext); result = script->Run(); } // Handle V8 internal error situation (Out-of-memory). if (result.IsEmpty()) return v8::Local<v8::Value>(); return result; }
static void addJudge (Tournament tournament) { char * nickname = strtok(NULL, " \n\t"); char * intHolder; getInt(intHolder,preference); JudgeByPreference judgeByPreference; switch(preference) { case 1: judgeByPreference = judgeByPreference1; break; case 2: judgeByPreference = judgeByPreference2; break; case 3: judgeByPreference = judgeByPreference3; break; default: mtmPrintErrorMessage(stdout,MTM_INVALID_INPUT_COMMAND_PARAMETERS); return; } TournamentResult result = tournamentAddJudge(tournament, nickname, judgeByPreference); if (result == TOURNAMENT_OUT_OF_MEMORY) { handleOutOfMemory(tournament); } }
static void reset (Tournament * tournament) { tournamentDestroy(*tournament); *tournament = tournamentCreate(NULL); if (*tournament == NULL) { handleOutOfMemory(*tournament); } }
static void addDish (Tournament tournament) { char * chefName = strtok(NULL," \n\t"); char * dishName = strtok(NULL," \n\t"); char * intHolder; getInt(intHolder,dishType) getInt(intHolder,sweetness) getInt(intHolder,sourness) getInt(intHolder,saltyness) getInt(intHolder,priority) Taste taste = {sweetness, sourness, saltyness}; TournamentResult result = tournamentAddDishToChef(tournament, chefName, dishName, dishType, taste, priority); switch(result) { case TOURNAMENT_OUT_OF_MEMORY: handleOutOfMemory(tournament); break; case TOURNAMENT_NO_SUCH_CHEF: mtmPrintErrorMessage(stderr, MTM_CHEF_NOT_FOUND); break; case TOURNAMENT_BAD_PARAM: mtmPrintErrorMessage(stderr, MTM_INVALID_INPUT_COMMAND_PARAMETERS); break; default: assert(result == TOURNAMENT_SUCCESS); } }
void V8AbstractEventListener::invokeEventHandler(ExecutionContext* context, Event* event, v8::Local<v8::Value> jsEvent) { // If jsEvent is empty, attempt to set it as a hidden value would crash v8. if (jsEvent.IsEmpty()) return; v8::Local<v8::Context> v8Context = toV8Context(context, world()); if (v8Context.IsEmpty()) return; // We push the event being processed into the global object, so that it can be exposed by DOMWindow's bindings. v8::Handle<v8::String> eventSymbol = v8AtomicString(v8Context->GetIsolate(), "event"); v8::Local<v8::Value> returnValue; { // Catch exceptions thrown in the event handler so they do not propagate to javascript code that caused the event to fire. v8::TryCatch tryCatch; tryCatch.SetVerbose(true); // Save the old 'event' property so we can restore it later. v8::Local<v8::Value> savedEvent = getHiddenValue(v8Context->GetIsolate(), v8Context->Global(), eventSymbol); tryCatch.Reset(); // Make the event available in the global object, so DOMWindow can expose it. setHiddenValue(v8Context->GetIsolate(), v8Context->Global(), eventSymbol, jsEvent); tryCatch.Reset(); returnValue = callListenerFunction(context, jsEvent, event); if (tryCatch.HasCaught()) event->target()->uncaughtExceptionInEventHandler(); if (!tryCatch.CanContinue()) { // Result of TerminateExecution(). if (context->isWorkerGlobalScope()) toWorkerGlobalScope(context)->script()->forbidExecution(); return; } tryCatch.Reset(); // Restore the old event. This must be done for all exit paths through this method. if (savedEvent.IsEmpty()) setHiddenValue(v8Context->GetIsolate(), v8Context->Global(), eventSymbol, v8::Undefined(v8Context->GetIsolate())); else setHiddenValue(v8Context->GetIsolate(), v8Context->Global(), eventSymbol, savedEvent); tryCatch.Reset(); } ASSERT(!handleOutOfMemory() || returnValue.IsEmpty()); if (returnValue.IsEmpty()) return; if (!returnValue->IsNull() && !returnValue->IsUndefined() && event->isBeforeUnloadEvent()) { V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<>, stringReturnValue, returnValue); toBeforeUnloadEvent(event)->setReturnValue(stringReturnValue); } if (m_isAttribute && shouldPreventDefault(returnValue)) event->preventDefault(); }
v8::Local<v8::Value> V8Proxy::runScript(v8::Handle<v8::Script> script) { if (script.IsEmpty()) return v8::Local<v8::Value>(); V8GCController::checkMemoryUsage(); if (V8RecursionScope::recursionLevel() >= kMaxRecursionDepth) return handleMaxRecursionDepthExceeded(); if (handleOutOfMemory()) ASSERT(script.IsEmpty()); // Keep Frame (and therefore ScriptController and V8Proxy) alive. RefPtr<Frame> protect(frame()); // Run the script and keep track of the current recursion depth. v8::Local<v8::Value> result; v8::TryCatch tryCatch; tryCatch.SetVerbose(true); { V8RecursionScope recursionScope(frame()->document()); result = script->Run(); } if (handleOutOfMemory()) ASSERT(result.IsEmpty()); // Handle V8 internal error situation (Out-of-memory). if (tryCatch.HasCaught()) { ASSERT(result.IsEmpty()); return v8::Local<v8::Value>(); } if (result.IsEmpty()) return v8::Local<v8::Value>(); if (v8::V8::IsDead()) handleFatalErrorInV8(); return result; }
int main(int argc,char *argv[]) { // add support for input & output files TournamentResult result; Tournament tournament = tournamentCreate(&result); if (result == TOURNAMENT_OUT_OF_MEMORY) { handleOutOfMemory(tournament); } char line[MAX_LEN+1]; strcpy(line,""); while ((fgets(line, sizeof(line), stdin)) != NULL) { proccessCommand(line,tournament); } tournamentDestroy(tournament); return 0; }
static void addChef (Tournament tournament) { char * name = strtok(NULL," \n\t"); if (name == NULL) { mtmPrintErrorMessage(stdout,MTM_INVALID_INPUT_COMMAND_PARAMETERS); return; } TournamentResult result; result = tournamentAddChef(tournament, name); if (result == TOURNAMENT_OUT_OF_MEMORY) { handleOutOfMemory(tournament); } if (result == TOURNAMENT_CHEF_ALREADY_EXISTS) { mtmPrintErrorMessage(stderr,MTM_CHEF_ALREADY_EXISTS); } }
static void printJudges (Tournament tournament) { char ** judges; int numberOfJudges; TournamentResult result = tournamentGetJudges(tournament, &judges, &numberOfJudges); if (result == TOURNAMENT_OUT_OF_MEMORY) { handleOutOfMemory(tournament); } if(result == TOURNAMENT_HAS_NO_JUDGES) { mtmPrintErrorMessage(stderr,MTM_NO_JUDGES); return; } mtmPrintAllJudges(stdout, judges, numberOfJudges); for (int i = 0; i < numberOfJudges; i++) { free(judges[i]); } free(judges); }
static void compete(Tournament tournament) { char* chefName1 = strtok(NULL," \n\t"); char* chefName2 = strtok(NULL," \n\t"); char** resigningJudges; int resigningCount; bool firstChefWins, secondChefWins; TournamentResult result = tournamentCompete(tournament, chefName1, chefName2, &resigningJudges, &resigningCount, &firstChefWins, &secondChefWins); switch(result) { case TOURNAMENT_OUT_OF_MEMORY: handleOutOfMemory(tournament); case TOURNAMENT_NO_SUCH_CHEF: mtmPrintErrorMessage(stderr, MTM_CHEF_NOT_FOUND); return; case TOURNAMENT_SAME_CHEF: mtmPrintErrorMessage(stderr, MTM_SAME_CHEF); return; case TOURNAMENT_CHEF_HAS_NO_DISHES: mtmPrintErrorMessage(stderr, MTM_CHEF_HAS_NO_DISHES); return; default: assert(result == TOURNAMENT_SUCCESS || result == TOURNAMENT_HAS_NO_JUDGES); } for(int i=0; i<resigningCount; i++) { mtmPrintJudgeResignationMessage(stdout,resigningJudges[i]); free(resigningJudges[i]); } free(resigningJudges); if(result == TOURNAMENT_HAS_NO_JUDGES) { mtmPrintErrorMessage(stderr,MTM_NO_JUDGES); return; } if(firstChefWins) { mtmPrintWinningAndLosingChefs(stdout, chefName1, chefName2); } else if(secondChefWins) { mtmPrintWinningAndLosingChefs(stdout, chefName2, chefName2); } else { mtmPrintTieBetweenTwoChefs(stdout, chefName1, chefName2); } }
Font XLoadFont(Display* display, _Xconst char* name) { // https://tronche.com/gui/x/xlib/graphics/font-metrics/XLoadFont.html SET_X_SERVER_REQUEST(display, X_OpenFont); XID font = ALLOC_XID(); if (font == None) { handleOutOfMemory(0, display, 0, 0); return None; } SET_XID_TYPE(font, FONT); int fontSize = FONT_SIZE; // TODO: Remove static font size // TODO: Handle "fixed" and "cursor" and others alike right. const char* fontPath = NULL; if (strcmp(name, "fixed") == 0 || strcmp(name, "cursor") == 0) { // This seems to be a common monospace font on most Android devices. fontPath = "/system/fonts/DroidSansMono.ttf"; } else { ssize_t index = findInArrayCmp(fontCache, (void *) name, &fontCmp); if (index != -1) { fontPath = ((FontCacheEntry*) fontCache->array[index])->filePath; } } if (fontPath == NULL) { FREE_XID(font); LOG("Font %s not found in cache!\n", name); handleError(0, display, None, 0, BadName, 0); return None; } SET_XID_VALUE(font, TTF_OpenFont(fontPath, fontSize)); if (GET_XID_VALUE(font) == NULL){ FREE_XID(font); LOG("Failed to load font %s!\n", name); handleError(0, display, None, 0, BadName, 0); return None; } return font; }
XFontStruct* XLoadQueryFont(Display* display, _Xconst char* name) { // https://tronche.com/gui/x/xlib/graphics/font-metrics/XLoadQueryFont.html Font fontId = XLoadFont(display, name); if (fontId == None) { return NULL; } TTF_Font* font = GET_FONT(fontId); SET_X_SERVER_REQUEST(display, X_QueryFont); XFontStruct* fontStruct = malloc(sizeof(XFontStruct)); if (fontStruct == NULL) { handleOutOfMemory(0, display, 0, 0); TTF_CloseFont(font); return NULL; } fontStruct->fid = fontId; fontStruct->ascent = TTF_FontAscent(font); fontStruct->descent = abs(TTF_FontDescent(font)); fontStruct->per_char = NULL; unsigned int numChars = 0; unsigned int i; for (i = 0; i < 65536 /* 2^16 */; i++) { if (TTF_GlyphIsProvided(font, (Uint16) i)) { if (numChars == 0) { fontStruct->min_char_or_byte2 = i; } fontStruct->max_char_or_byte2 = i; numChars++; } } // if (numChars >= 256) { // fontStruct->min_byte1 = fontStruct->min_char_or_byte2 / 256; // fontStruct->max_byte1 = fontStruct->max_char_or_byte2 / 256; // fontStruct->min_char_or_byte2 = 0; // } else { fontStruct->min_byte1 = 0; fontStruct->max_byte1 = 0; // } // TODO: This is debugging fontStruct->max_char_or_byte2 = 255; // Build per_char int monospace = TTF_FontFaceIsFixedWidth(font); XCharStruct charStruct; if (!monospace) { fontStruct->per_char = malloc(sizeof(XCharStruct) * numChars); if (fontStruct->per_char == NULL) { handleOutOfMemory(0, display, 0, 0); XFreeFont(display, fontStruct); return NULL; } charStruct = fontStruct->per_char[0]; } if (fillXCharStruct(font, fontStruct->min_char_or_byte2, &charStruct) == False) { XFreeFont(display, fontStruct); return NULL; } fontStruct->max_bounds = charStruct; fontStruct->min_bounds = charStruct; if (monospace) { fontStruct->per_char = NULL; } else { int counter = 1; for (i = fontStruct->min_char_or_byte2 + 1; i < 65536 /* 2^16 */; ++i) { if (TTF_GlyphIsProvided(font, (Uint16) i)) { charStruct = fontStruct->per_char[counter]; if (fillXCharStruct(font, i, &charStruct) == False) { XFreeFont(display, fontStruct); return NULL; } // I think rbearing (aka. advance) is the value that matters here if (fontStruct->max_bounds.rbearing < charStruct.rbearing) { fontStruct->max_bounds = charStruct; } else if (fontStruct->min_bounds.rbearing > charStruct.rbearing) { fontStruct->max_bounds = charStruct; } counter++; } } } return fontStruct; }
void V8AbstractEventListener::invokeEventHandler(ScriptExecutionContext* context, Event* event, v8::Handle<v8::Value> jsEvent) { // If jsEvent is empty, attempt to set it as a hidden value would crash v8. if (jsEvent.IsEmpty()) return; v8::Local<v8::Context> v8Context = toV8Context(context, worldContext()); if (v8Context.IsEmpty()) return; // We push the event being processed into the global object, so that it can be exposed by DOMWindow's bindings. v8::Handle<v8::String> eventSymbol = V8HiddenPropertyName::event(); v8::Local<v8::Value> returnValue; // In beforeunload/unload handlers, we want to avoid sleeps which do tight loops of calling Date.getTime(). if (event->type() == eventNames().beforeunloadEvent || event->type() == eventNames().unloadEvent) DateExtension::get()->setAllowSleep(false); { // Catch exceptions thrown in the event handler so they do not propagate to javascript code that caused the event to fire. v8::TryCatch tryCatch; tryCatch.SetVerbose(true); // Save the old 'event' property so we can restore it later. v8::Local<v8::Value> savedEvent = v8Context->Global()->GetHiddenValue(eventSymbol); tryCatch.Reset(); // Make the event available in the global object, so DOMWindow can expose it. v8Context->Global()->SetHiddenValue(eventSymbol, jsEvent); tryCatch.Reset(); returnValue = callListenerFunction(context, jsEvent, event); if (tryCatch.HasCaught()) event->target()->uncaughtExceptionInEventHandler(); if (!tryCatch.CanContinue()) { // Result of TerminateExecution(). #if ENABLE(WORKERS) if (context->isWorkerContext()) static_cast<WorkerContext*>(context)->script()->forbidExecution(); #endif return; } tryCatch.Reset(); // Restore the old event. This must be done for all exit paths through this method. if (savedEvent.IsEmpty()) v8Context->Global()->SetHiddenValue(eventSymbol, v8::Undefined()); else v8Context->Global()->SetHiddenValue(eventSymbol, savedEvent); tryCatch.Reset(); } if (event->type() == eventNames().beforeunloadEvent || event->type() == eventNames().unloadEvent) DateExtension::get()->setAllowSleep(true); ASSERT(!handleOutOfMemory() || returnValue.IsEmpty()); if (returnValue.IsEmpty()) return; if (!returnValue->IsNull() && !returnValue->IsUndefined() && event->storesResultAsString()) event->storeResult(toWebCoreString(returnValue)); if (m_isAttribute && shouldPreventDefault(returnValue)) event->preventDefault(); }