//---------------------------------------------------------------------------- // InitializeContentScripting STDMETHODIMP CAnchoAddon::InitializeContentScripting(IWebBrowser2* pBrowser, BSTR bstrUrl, documentLoadPhase aPhase) { IF_FAILED_RET(initializeEnvironment()); // content script handling happens here // no frame handling // TODO: decide how to handle frames if (!m_pWebBrowser.IsEqualObject(pBrowser)) { return S_OK; } if (aPhase != documentLoadEnd) { return S_OK; } cleanupScripting(); // get content our API IF_FAILED_RET(m_pAddonBackground->GetContentInfo(m_InstanceID, bstrUrl, &m_pContentInfo)); CString s; s.Format(_T("Ancho content [%s] [%i]"), m_sExtensionName, m_InstanceID); IF_FAILED_RET(m_Magpie->Init((LPWSTR)(LPCWSTR)s)); // add a loader for scripts in the extension filesystem IF_FAILED_RET(m_Magpie->AddFilesystemScriptLoader((LPWSTR)(LPCWSTR)m_sExtensionPath)); // inject items: chrome, console and window with global members CComQIPtr<IWebBrowser2> pWebBrowser(pBrowser); ATLASSERT(pWebBrowser); CIDispatchHelper contentInfo(m_pContentInfo); CComVariant jsObj; IF_FAILED_RET((contentInfo.Get<CComVariant, VT_DISPATCH, IDispatch*>(L"api", jsObj))); IF_FAILED_RET(DOMWindowWrapper::createInstance(pWebBrowser, m_wrappedWindow)); m_Magpie->AddNamedItem(L"chrome", jsObj.pdispVal, SCRIPTITEM_ISVISIBLE|SCRIPTITEM_CODEONLY); m_Magpie->AddNamedItem(L"window", m_wrappedWindow, SCRIPTITEM_ISVISIBLE|SCRIPTITEM_GLOBALMEMBERS); CIDispatchHelper window = m_wrappedWindow; CComPtr<IDispatchEx> pRequest; IF_FAILED_RET(pRequest.CoCreateInstance(__uuidof(AnchoXmlHttpRequest))); IF_FAILED_RET(window.SetProperty((LPOLESTR)L"XMLHttpRequest", CComVariant(pRequest.p))); m_Magpie->AddNamedItem(L"XMLHttpRequest", pRequest, SCRIPTITEM_ISVISIBLE|SCRIPTITEM_CODEONLY); // get the name(s) of content scripts from manifest and run them in order IF_FAILED_RET((contentInfo.Get<CComVariant, VT_DISPATCH, IDispatch*>(L"scripts", jsObj))); VariantVector scripts; IF_FAILED_RET(addJSArrayToVariantVector(jsObj.pdispVal, scripts)); for(VariantVector::iterator it = scripts.begin(); it != scripts.end(); ++it) { if( it->vt == VT_BSTR ) { m_Magpie->ExecuteGlobal(it->bstrVal); } } return S_OK; }
virtual bool initialize() { if (!initializeShaders()) { return false; } if (!initializeEnvironment()) { return false; } camera.position = matrix3x1(0, 40, 0); return true; }
void SpaceshipGame::initialize() { // TODO: Not working on iOS // Display the gameplay splash screen for at least 1 second. displayScreen(this, &SpaceshipGame::drawSplash, NULL, 1000L); // Create our render state block that will be reused across all materials _stateBlock = RenderState::StateBlock::create(); _stateBlock->setDepthTest(true); _stateBlock->setCullFace(true); _stateBlock->setBlend(true); _stateBlock->setBlendSrc(RenderState::BLEND_SRC_ALPHA); _stateBlock->setBlendDst(RenderState::BLEND_ONE_MINUS_SRC_ALPHA); // Load our scene from file Bundle* bundle = Bundle::create("res/spaceship.gpb"); _scene = bundle->loadScene(); SAFE_RELEASE(bundle); // Update the aspect ratio for our scene's camera to match the current device resolution _scene->getActiveCamera()->setAspectRatio((float)getWidth() / (float)getHeight()); // Initialize scene data initializeSpaceship(); initializeEnvironment(); // Create a background audio track _backgroundSound = AudioSource::create("res/background.ogg"); if (_backgroundSound) _backgroundSound->setLooped(true); // Create font _font = Font::create("res/airstrip28.gpb"); // Store camera node _cameraNode = _scene->findNode("camera1"); // Store initial ship and camera positions _initialShipPos = _shipGroupNode->getTranslation(); _initialShipRot = _shipGroupNode->getRotation(); _initialCameraPos = _cameraNode->getTranslation(); }
//---------------------------------------------------------------------------- // InitializeExtensionScripting STDMETHODIMP CAnchoAddon::InitializeExtensionScripting(BSTR bstrUrl) { IF_FAILED_RET(initializeEnvironment()); cleanupScripting(); // get content our API IF_FAILED_RET(m_pAddonBackground->GetContentInfo(m_InstanceID, bstrUrl, &m_pContentInfo)); CIDispatchHelper contentInfo(m_pContentInfo); CComVariant api; IF_FAILED_RET((contentInfo.Get<CComVariant, VT_DISPATCH, IDispatch*>(L"api", api))); CIDispatchHelper script = CIDispatchHelper::GetScriptDispatch(m_pWebBrowser); IF_FAILED_RET(script.SetPropertyByRef(L"chrome", api)); CComVariant console; IF_FAILED_RET((contentInfo.Get<CComVariant, VT_DISPATCH, IDispatch*>(L"console", console))); IF_FAILED_RET(script.SetPropertyByRef(L"console", console)); return S_OK; }
int main(int argc, char** argv) { std::vector<std::string> cases = { "\"Hello\"", "1", "100", "10000", "#t", "#f", "(+ 1 2)", "(+ (+ 1 2) 4)", "(+ (+ 1 2) (+ 3 4) (+ 1 2 3 4 5 6))", "(+)", "(-)", "(- (+ 1 2) 3)", "(* 1 2 3 4 5 0)", "(* (+ 1 2) (+ 3 4))", "(*)", // => 1 "(/)", // => 1 "(/ 2)", // => 0 "(write \"hello world!\")", "(/ 8 4)", "(boolean? #t)", // => #t "(boolean? #f)", // => #t "(boolean? \"hello\")", // => #f "(number? 1234)", // => #t "(string? \"hello\")", // => #t "(number? \"1234\")", // => #f "'()", // => '() "'(1 2 3)", // => '(1 2 3) "(null? '())", // => #t "(pair? '(1 2))", // => #t "(pair? '(1 2 3 4 5 6))", // => #t "#(1 2 3 4 5)", // => #(1 2 3 4 5) "(vector-length '#(1 2 3 4 5))", // => 5 "(vector-ref '#(1 1 2 3 5 8 13 21) 5)", // => 8 "(vector-set! '#(0 1 2 3 4 5) 3 27)", // this should raise &assertion exception for trying to set in constant vector "(if #t 1 2)", // => 1 "(if #f 1 2)", // => 2 "(if #t (+ 1 3) (+ 1 5))", // => 4 "(if #f (+ 1 3) (+ 1 5))", // => 6 "(if 1 1 2)", // => 1 "(if #t (if #f 1 2) (if #f 3 4))", // => 2 "(quote (1 2 3))", // => '(1 2 3) "(begin (+ 1 2) (+ 3 4))", // => 7 "(define a 23)", // => '() "(begin (define a 23) (+ a 1))", // => 24 "(begin (define add1 (lambda (x) (+ x 1))) (add1 23))", // => 24 "(if (string? 123) 1 2)", // => 2 "(begin (define a 23) (define b 24) (+ a b))", // => 47 "(begin (define a 23) (define b 24) (define plus (lambda (x y) (+ x y))) (plus a b))", // => 47 "(eq? 0 0)", // => #t "(eq? 1 1)", // => #t "(eq? 1 2)", // => #f "(if (eq? 1 1) 1 2)", // => 1 "(begin (define fact (lambda (x) (if (eq? x 0) 1 2))) (fact 4))", // => 2 "(begin (define fact (lambda (x) (if (eq? x 0) 1 2))) (fact (- 4 4)))", // => 1 "(begin (define fact (lambda (x) (if (eq? x 0) 1 (* x (- x 1))))) (fact 4))", // => 1 "(begin (define factorial (lambda (x) (if (eq? x 0) 1 (* x (factorial (- x 1)))))) (factorial 0))", // => 1 "(begin (define factorial (lambda (x) (if (eq? x 0) 1 (* x (factorial (- x 1)))))) (factorial 1))", // => 1 "(begin (define factorial (lambda (x) (if (eq? x 0) 1 (* x (factorial (- x 1)))))) (factorial 4))", // => 24 "(begin " " (define fib (lambda (n) " " (if (< n 2) " " n " " (+ (fib (- n 1)) (fib (- n 2))) " " ) " " )) " "(fib 2)) ", // => 1 "(begin " " (define fib (lambda (n) " " (if (< n 2) " " n " " (+ (fib (- n 1)) (fib (- n 2))) " " ) " " )) " "(fib 4)) ", // => 3 "(begin " " (define fib (lambda (n) " " (if (< n 2) " " n " " (+ (fib (- n 1)) (fib (- n 2))) " " ) " " )) " "(fib 8)) " // => 21 }; for (const auto& c : cases) { try { Environment env; initializeEnvironment(env); ObjectPtr res = std::move(process(c.c_str(), env)); if (res) { std::cout << " " << *res << std::endl; } } catch (std::runtime_error& ex) { std::cout << "Exception: " << ex.what() << "\n"; } } return 0; }
/* This is where the program begins. */ int main(int argc, char *argv[]) { /* All variables that we need */ char *input, **words; int i = 0, j = 0, running = 1; FILE *inFile = NULL; pipeFlag = 0; defin = dup(STDIN_FILENO); defout = dup(STDOUT_FILENO); initializeEnvironment(); if (argc > 1) { for (i = 1; i < argc; i ++) { if (strcmp(argv[i], "-x") == 0) { displayCommand = 1; } else if (strcmp(argv[i], "-d") == 0) { i++; if (i >= argc) { printf("./xssh [-x] [-d level] [file [arg] …]\n"); running = 0; exit_status = -1; } else { debug_level = atoi(argv[i]); } } else { if (inFile == NULL) { if ((inFile = fopen(argv[i], "r")) == NULL) { printf("The file name is invalid.\n"); running = 0; exit_status = -1; } else { /* grab file arguments */ exit_status = run_script(inFile); fclose(inFile); } } } } } /* Initialize child capacity, child count, and array of child pids */ child_cap = 1; child_count = 0; child_pids = malloc(child_cap * sizeof(pid_t)); while (running) { i = 0; redirect_in = 0; pipeFlag = 0; dup2(defin, STDIN_FILENO); dup2(defout, STDOUT_FILENO); /* get the input from user. */ input = readline(">> "); if (strcmp(input, "") == 0) { continue; } add_history(input); words = parse_cmd(input, &i); if (pipeFlag) { running = run_pipe_cmd(i, words); pipeFlag = 0; } else { running = run_cmd(i, words); } } for (j = 0; j < i; j++) { free(words[i]); } free(words); free(input); return exit_status; }