JsErrorCode ChakraHost::RunSerializedScript(const wchar_t* szPath, const wchar_t* szSerializedPath, const wchar_t* szSourceUri, JsValueRef* result) { HANDLE hFile = NULL; HANDLE hMap = NULL; JsErrorCode status = JsNoError; ULONG bufferSize = 0L; BYTE* buffer = nullptr; wchar_t* szScriptBuffer = nullptr; IfFailRet(LoadFileContents(szPath, &szScriptBuffer)); if (!CompareLastWrite(szSerializedPath, szPath)) { IfFailRet(JsSerializeScript(szScriptBuffer, buffer, &bufferSize)); buffer = new BYTE[bufferSize]; IfFailRet(JsSerializeScript(szScriptBuffer, buffer, &bufferSize)); FILE* file; _wfopen_s(&file, szSerializedPath, L"wb"); fwrite(buffer, sizeof(BYTE), bufferSize, file); fclose(file); } else { IfFailRet(LoadByteCode(szSerializedPath, &buffer, &hFile, &hMap)); } SerializedSourceContext* context = new SerializedSourceContext(); context->byteBuffer = buffer; context->scriptBuffer = szScriptBuffer; context->fileHandle = hFile; context->mapHandle = hMap; IfFailRet(JsRunSerializedScriptWithCallback(&LoadSourceCallback, &UnloadSourceCallback, buffer, (JsSourceContext)context, szSourceUri, result)); return status; }
//Clone each of our sections. This will cause a deep copy of the sections HRESULT PESectionMan::cloneInstance(PESectionMan *destination) { _ASSERTE(destination); PESection *pSection; PESection **destPtr; HRESULT hr = NOERROR; //Copy each of the sections for (PESection** ptr = sectStart; ptr < sectCur; ptr++) { destPtr = destination->sectStart; pSection = NULL; // try to find the matching section by name for (; destPtr < destination->sectCur; destPtr++) { if (strcmp((*destPtr)->m_name, (*ptr)->m_name) == 0) { pSection = *destPtr; break; } } if (destPtr >= destination->sectCur) { // cannot find a section in the destination with matching name // so create one! IfFailRet( destination->getSectionCreate((*ptr)->m_name, (*ptr)->flags(), &pSection) ); } if (pSection) IfFailRet( (*ptr)->cloneInstance(pSection) ); } //destination->sectEnd=destination->sectStart + (sectEnd-sectStart); return S_OK; }
JsErrorCode ChakraHost::Destroy() { IfFailRet(JsSetCurrentContext(JS_INVALID_REFERENCE)); IfFailRet(JsDisposeRuntime(runtime)); return JsNoError; }
HRESULT CeeFileGenWriter::generateImage(void **ppImage) { HRESULT hr; if (!m_fixed) IfFailRet(fixup()); LPWSTR outputFileName = m_outputFileName; if (! outputFileName && ppImage == NULL) { if (m_comImageFlags & COMIMAGE_FLAGS_IL_LIBRARY) outputFileName = L"output.ill"; else if (m_dllSwitch) outputFileName = L"output.dll"; else if (m_objSwitch) outputFileName = L"output.obj"; else outputFileName = L"output.exe"; } // output file name and ppImage are mutually exclusive _ASSERTE((NULL == outputFileName && ppImage != NULL) || (outputFileName != NULL && NULL == ppImage)); if (outputFileName != NULL) IfFailRet(getPEWriter().write(outputFileName)); else IfFailRet(getPEWriter().write(ppImage)); return S_OK; } // HRESULT CeeFileGenWriter::generateImage()
//***************************************************************************** // Get the record from a Delta MetaData that corresponds to the actual record. //***************************************************************************** __checkReturn HRESULT CMiniMdRW::GetDeltaRecord( ULONG ixTbl, // Table. ULONG iRid, // Record in the table. void **ppRecord) { HRESULT hr; ULONG iMap; // RID in map table. ENCMapRec *pMap; // Row in map table. *ppRecord = NULL; // If no remap, just return record directly. if ((m_Schema.m_cRecs[TBL_ENCMap] == 0) || (ixTbl == TBL_Module) || !IsMinimalDelta()) { return getRow(ixTbl, iRid, ppRecord); } // Use the remap table to find the physical row containing this logical row. iMap = (*m_rENCRecs)[ixTbl]; IfFailRet(GetENCMapRecord(iMap, &pMap)); // Search for desired record. while ((TblFromRecId(pMap->GetToken()) == ixTbl) && (RidFromRecId(pMap->GetToken()) < iRid)) { IfFailRet(GetENCMapRecord(++iMap, &pMap)); } _ASSERTE((TblFromRecId(pMap->GetToken()) == ixTbl) && (RidFromRecId(pMap->GetToken()) == iRid)); // Relative position within table's group in map is physical rid. iRid = iMap - (*m_rENCRecs)[ixTbl] + 1; return getRow(ixTbl, iRid, ppRecord); } // CMiniMdRW::GetDeltaRecord
HRESULT CeeFileGenWriter::emitLibraryName(IMetaDataEmit *emitter) { HRESULT hr; IfFailRet(emitter->SetModuleProps(m_libraryName)); // Set the GUID as a custom attribute, if it is not NULL_GUID. if (m_libraryGuid != GUID_NULL) { static COR_SIGNATURE _SIG[] = INTEROP_GUID_SIG; mdTypeRef tr; mdMemberRef mr; WCHAR wzGuid[40]; BYTE rgCA[50]; IfFailRet(emitter->DefineTypeRefByName(mdTypeRefNil, INTEROP_GUID_TYPE_W, &tr)); IfFailRet(emitter->DefineMemberRef(tr, L".ctor", _SIG, sizeof(_SIG), &mr)); StringFromGUID2(m_libraryGuid, wzGuid, lengthof(wzGuid)); memset(rgCA, 0, sizeof(rgCA)); // Tag is 0x0001 rgCA[0] = 1; // Length of GUID string is 36 characters. rgCA[2] = 0x24; // Convert 36 characters, skipping opening {, into 3rd byte of buffer. WszWideCharToMultiByte(CP_ACP,0, wzGuid+1,36, reinterpret_cast<char*>(&rgCA[3]),36, 0,0); hr = emitter->DefineCustomAttribute(1,mr,rgCA,41,0); } return (hr); } // HRESULT CeeFileGenWriter::emitLibraryName()
JsErrorCode ChakraHost::SetGlobalVariable(const wchar_t* szPropertyName, JsValueRef value) { JsPropertyIdRef globalVarId; IfFailRet(JsGetPropertyIdFromName(szPropertyName, &globalVarId)); IfFailRet(JsSetProperty(globalObject, globalVarId, value, true)); return JsNoError; }
HRESULT CeeFileGenWriter::fixup() { HRESULT hr; m_fixed = true; if (!m_linked) IfFailRet(link()); CeeGenTokenMapper *pMapper = getTokenMapper(); // Apply token remaps if there are any. if (! m_fTokenMapSupported && pMapper != NULL) { IMetaDataImport *pImport; hr = pMapper->GetMetaData(&pImport); _ASSERTE(SUCCEEDED(hr)); hr = MapTokens(pMapper, pImport); pImport->Release(); } // remap the entry point if entry point token has been moved if (pMapper != NULL && !m_objSwitch) { mdToken tk = m_entryPoint; pMapper->HasTokenMoved(tk, tk); m_corHeader->EntryPointToken = VAL32(tk); } IfFailRet(getPEWriter().fixup(pMapper)); return S_OK; } // HRESULT CeeFileGenWriter::fixup()
HRESULT Target_CLiteWeightStgdb_CMiniMdRW::ReadFrom(DataTargetReader & reader) { HRESULT hr = S_OK; IfFailRet(reader.Read(&m_MiniMd)); IfFailRet(reader.ReadPointer(&m_pvMd)); IfFailRet(reader.Read32(&m_cbMd)); return S_OK; }
HRESULT Target_StgBlobPool::ReadFrom(DataTargetReader & reader) { HRESULT hr = S_OK; IfFailRet(Target_StgPool::ReadFrom(reader)); reader.AlignBase(); IfFailRet(reader.Read(&m_Hash)); return S_OK; }
HRESULT Target_RecordPool::ReadFrom(DataTargetReader & reader) { HRESULT hr = S_OK; IfFailRet(Target_StgPool::ReadFrom(reader)); reader.AlignBase(); IfFailRet(reader.Read32(&m_cbRec)); return S_OK; }
HRESULT Target_CGuidPoolHash::ReadFrom(DataTargetReader & reader) { HRESULT hr = S_OK; IfFailRet(Target_CChainedHash::ReadFrom(reader)); reader.AlignBase(); IfFailRet(reader.ReadPointer(&m_Pool)); return S_OK; }
HRESULT Target_CMiniColDef::ReadFrom(DataTargetReader & reader) { HRESULT hr = S_OK; IfFailRet(reader.Read8(&m_Type)); IfFailRet(reader.Read8(&m_oColumn)); IfFailRet(reader.Read8(&m_cbColumn)); return S_OK; }
HRESULT Target_StgPoolSeg::ReadFrom(DataTargetReader & reader) { HRESULT hr = S_OK; IfFailRet(reader.ReadPointer(&m_pSegData)); IfFailRet(reader.ReadPointer(&m_pNextSeg)); IfFailRet(reader.Read32(&m_cbSegSize)); IfFailRet(reader.Read32(&m_cbSegNext)); return S_OK; }
HRESULT Target_StgPoolReadOnly::ReadFrom(DataTargetReader & reader) { HRESULT hr = S_OK; IfFailRet(reader.SkipPointer()); // __vfptr IfFailRet(Target_StgPoolSeg::ReadFrom(reader)); reader.AlignBase(); IfFailRet(reader.Read(&m_HotHeap)); return S_OK; }
JsErrorCode StringifyJsObject(JsValueRef value, USHORT depth, std::set<JsValueRef> seen) { std::pair<std::set<JsValueRef>::iterator, bool> ret; ret = seen.insert(value); if (!ret.second) { OutputDebugStringW(DEFAULT_RECURSIVE_VALUE); return JsNoError; } if (depth > DEFAULT_MAX_DEPTH) { OutputDebugStringW(DEFAULT_TRUNCATED_VALUE); return JsNoError; } JsValueRef props; IfFailRet(JsGetOwnPropertyNames(value, &props)); JsPropertyIdRef lengthId; JsValueRef lengthProp; int lengthValue; IfFailRet(JsGetPropertyIdFromName(L"length", &lengthId)); IfFailRet(JsGetProperty(props, lengthId, &lengthProp)); IfFailRet(JsNumberToInt(lengthProp, &lengthValue)); OutputDebugStringW(L"{ "); for (int i = 0; i < lengthValue; i++) { JsPropertyIdRef propId; JsValueRef index, indexResult, prop; const wchar_t* szProp; size_t sProp; IfFailRet(JsIntToNumber(i, &index)); IfFailRet(JsGetIndexedProperty(props, index, &indexResult)); IfFailRet(JsStringToPointer(indexResult, &szProp, &sProp)); IfFailRet(JsGetPropertyIdFromName(szProp, &propId)); IfFailRet(JsGetProperty(value, propId, &prop)); OutputDebugStringW(szProp); OutputDebugStringW(L": "); IfFailRet(StringifyJsValue(prop, depth + 1, seen)); if (i != lengthValue - 1) { OutputDebugStringW(L", "); } else { OutputDebugStringW(L" "); } } OutputDebugStringW(L"}"); return JsNoError; }
HRESULT Target_CMiniMdSchema::ReadFrom(DataTargetReader & reader) { HRESULT hr = S_OK; IfFailRet(Target_CMiniMdSchemaBase::ReadFrom(reader)); reader.AlignBase(); for (int i = 0; i < TBL_COUNT; i++) IfFailRet(reader.Read32(&(m_cRecs[i]))); IfFailRet(reader.Read32(&m_ulExtra)); return S_OK; }
JsErrorCode StringifyToString(JsValueRef value) { JsValueRef resultString; const wchar_t* szStr; size_t sStr; IfFailRet(JsConvertValueToString(value, &resultString)); IfFailRet(JsStringToPointer(resultString, &szStr, &sStr)); OutputDebugStringW(szStr); return JsNoError; }
JsErrorCode DefineHostCallback(JsValueRef globalObject, const wchar_t *callbackName, JsNativeFunction callback, void *callbackState) { JsPropertyIdRef propertyId; IfFailRet(JsGetPropertyIdFromName(callbackName, &propertyId)); JsValueRef function; IfFailRet(JsCreateFunction(callback, callbackState, &function)); IfFailRet(JsSetProperty(globalObject, propertyId, function, true)); return JsNoError; }
__checkReturn HRESULT WINAPI CrossgenRoResolveNamespace( const LPCWSTR wszNamespace, DWORD * pcMetadataFiles, SString ** ppMetadataFiles) { LIMITED_METHOD_CONTRACT; HRESULT hr = S_OK; if (IsWindowsNamespace(wszNamespace)) { DWORD cAppPaths = g_wszWindowsNamespaceDirectories->GetCount(); for (DWORD i = 0; i < cAppPaths; i++) { // Returns S_FALSE on file not found so we continue proving app directory graph IfFailRet(FindNamespaceFileInDirectory( wszNamespace, g_wszWindowsNamespaceDirectories->Get(i).GetUnicode(), pcMetadataFiles, ppMetadataFiles)); if (hr == S_OK) { return hr; } } } else { DWORD cAppPaths = g_wszUserNamespaceDirectories->GetCount(); for (DWORD i = 0; i < cAppPaths; i++) { // Returns S_FALSE on file not found so we continue proving app directory graph IfFailRet(FindNamespaceFileInDirectory( wszNamespace, g_wszUserNamespaceDirectories->Get(i).GetUnicode(), pcMetadataFiles, ppMetadataFiles)); if (hr == S_OK) { return hr; } } } return hr; } // RoResolveNamespace
int coreclr_execute_assembly( void* hostHandle, unsigned int domainId, int argc, const char** argv, const char* managedAssemblyPath, unsigned int* exitCode) { if (exitCode == NULL) { return HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER); } *exitCode = -1; ICLRRuntimeHost2* host = reinterpret_cast<ICLRRuntimeHost2*>(hostHandle); ConstWStringArrayHolder argvW; argvW.Set(StringArrayToUnicode(argc, argv), argc); ConstWStringHolder managedAssemblyPathW = StringToUnicode(managedAssemblyPath); HRESULT hr = host->ExecuteAssembly(domainId, managedAssemblyPathW, argc, argvW, (DWORD *)exitCode); IfFailRet(hr); return hr; }
JsErrorCode StringifyJsBoolean(JsValueRef value) { bool bResult; IfFailRet(JsBooleanToBool(value, &bResult)); OutputDebugStringW(bResult ? L"true" : L"false"); return JsNoError; }
// Saves the active watch list together with the current evaluations as // a new persisted watch list HRESULT WatchCmd::SaveList(__in_z WCHAR* pSaveName) { HRESULT Status = S_OK; INIT_API_EE(); INIT_API_DAC(); IfFailRet(InitCorDebugInterface()); RemoveList(pSaveName); PersistList* pList = new PersistList(); wcsncpy_s(pList->pName, MAX_EXPRESSION, pSaveName, _TRUNCATE); pList->pHeadExpr = NULL; PersistCallbackData data; data.ppNext = &(pList->pHeadExpr); WatchExpression* pExpression = pExpressionListHead; while(pExpression != NULL) { ExpressionNode* pResult = NULL; if(SUCCEEDED(Status = ExpressionNode::CreateExpressionNode(pExpression->pExpression, &pResult))) { pResult->DFSVisit(PersistCallback, (VOID*)&data); delete pResult; } pExpression = pExpression->pNext; } pList->pNext = pPersistListHead; pPersistListHead = pList; return Status; }
HRESULT FusionBind::EmitToken(IMetaDataAssemblyEmit *pEmit, mdAssemblyRef *pToken) { HRESULT hr; ASSEMBLYMETADATA AMD; IfFailRet(ParseName()); AMD.usMajorVersion = m_context.usMajorVersion; AMD.usMinorVersion = m_context.usMinorVersion; AMD.usBuildNumber = m_context.usBuildNumber; AMD.usRevisionNumber = m_context.usRevisionNumber; if (m_context.szLocale) { AMD.cbLocale = MultiByteToWideChar(CP_ACP, 0, m_context.szLocale, -1, NULL, 0); AMD.szLocale = (LPWSTR) alloca(AMD.cbLocale); MultiByteToWideChar(CP_ACP, 0, m_context.szLocale, -1, AMD.szLocale, AMD.cbLocale); } else { AMD.cbLocale = 0; AMD.szLocale = NULL; } long pwNameLen = WszMultiByteToWideChar(CP_UTF8, 0, m_pAssemblyName, -1, 0, 0); CQuickBytes qb; LPWSTR pwName = (LPWSTR) qb.Alloc(pwNameLen*sizeof(WCHAR)); WszMultiByteToWideChar(CP_UTF8, 0, m_pAssemblyName, -1, pwName, pwNameLen); return pEmit->DefineAssemblyRef(m_pbPublicKeyOrToken, m_cbPublicKeyOrToken, pwName, &AMD, NULL, 0, m_dwFlags, pToken); }
//***************************************************************************** // Set the cached Internal interface. This function will return an Error is the // current cached internal interface is not empty and trying set a non-empty internal // interface. One RegMeta will only associated // with one Internal Object. Unless we have bugs somewhere else. It will QI on the // IUnknown for the IMDInternalImport. If this failed, error will be returned. // Note: Caller should take a write lock // // This does addref the importer (the public and private importers maintain // weak references to each other). // // Implements internal API code:IMetaDataHelper::SetCachedInternalInterface. //***************************************************************************** HRESULT RegMeta::SetCachedInternalInterface(IUnknown *pUnk) { HRESULT hr = NOERROR; IMDInternalImport *pInternal = NULL; if (pUnk) { if (m_pInternalImport) { _ASSERTE(!"Bad state!"); } IfFailRet( pUnk->QueryInterface(IID_IMDInternalImport, (void **) &pInternal) ); // Should be non-null _ASSERTE(pInternal); m_pInternalImport = pInternal; // We don't want to add ref the internal interface, so undo the AddRef() from the QI. pInternal->Release(); } else { // Internal interface is going away before the public interface. Take ownership on the // reader writer lock. m_fOwnSem = true; m_pInternalImport = NULL; } return hr; } // RegMeta::SetCachedInternalInterface
HRESULT FusionBind::ParseName() { HRESULT hr = S_OK; if (m_fParsed || !m_pAssemblyName) return S_OK; TIMELINE_START(FUSIONBIND, ("ParseName %s", m_pAssemblyName)); IAssemblyName *pName; CQuickBytes qb; long pwNameLen = WszMultiByteToWideChar(CP_UTF8, 0, m_pAssemblyName, -1, 0, 0); LPWSTR pwName = (LPWSTR) qb.Alloc(pwNameLen*sizeof(WCHAR)); WszMultiByteToWideChar(CP_UTF8, 0, m_pAssemblyName, -1, pwName, pwNameLen); IfFailRet(CreateAssemblyNameObject(&pName, pwName, CANOF_PARSE_DISPLAY_NAME, NULL)); if (m_ownedFlags & NAME_OWNED) delete [] m_pAssemblyName; m_pAssemblyName = NULL; hr = Init(pName); pName->Release(); TIMELINE_END(FUSIONBIND, ("ParseName %s", m_pAssemblyName)); return hr; }
void RegistFunction( LPCWSTR function_name, JSFUNCTION func ) { JsSetCurrentContext(jscontext); IfFailRet(DefineHostCallback(ghostObject, function_name, func, nullptr)); JsSetCurrentContext(JS_INVALID_REFERENCE); }
JsErrorCode StringifyJsString(JsValueRef value) { const wchar_t* szResult; size_t sResult; IfFailRet(JsStringToPointer(value, &szResult, &sResult)); OutputDebugStringW(szResult); return JsNoError; }
HRESULT DataTargetReader::ReadPointer(TargetObject* pTargetObject) { HRESULT hr = S_OK; CORDB_ADDRESS pointerValue; IfFailRet(ReadPointer(&pointerValue)); DataTargetReader reader = CreateReaderAt(pointerValue); return pTargetObject->ReadFrom(reader); }
HRESULT PESectionMan::applyRelocs(CeeGenTokenMapper *pTokenMapper) { HRESULT hr; // Cycle through each of the sections for(PESection ** ppCurSection = sectStart; ppCurSection < sectCur; ppCurSection++) { IfFailRet((*ppCurSection)->applyRelocs(pTokenMapper)); } // End sections return S_OK; }