1.1 --- a/core/systems/PluginSys.cpp Mon Jul 07 06:24:54 2008 +0000
1.2 +++ b/core/systems/PluginSys.cpp Mon Jul 07 06:25:15 2008 +0000
1.3 @@ -58,7 +58,7 @@
1.4 m_type = PluginType_Private;
1.5 m_status = Plugin_Uncompiled;
1.6 m_serial = ++MySerial;
1.7 - m_plugin = NULL;
1.8 + m_pRuntime = NULL;
1.9 m_errormsg[256] = '\0';
1.10 snprintf(m_filename, sizeof(m_filename), "%s", file);
1.11 m_handle = 0;
1.12 @@ -82,27 +82,12 @@
1.13 g_ShareSys.DestroyIdentity(m_ident);
1.14 }
1.15
1.16 - if (m_ctx.base)
1.17 - {
1.18 - delete m_ctx.base;
1.19 - m_ctx.base = NULL;
1.20 - }
1.21 - if (m_ctx.ctx)
1.22 + if (m_pRuntime != NULL)
1.23 {
1.24 - m_ctx.vm->FreeContext(m_ctx.ctx);
1.25 - m_ctx.ctx = NULL;
1.26 - }
1.27 - if (m_ctx.co)
1.28 - {
1.29 - m_ctx.vm->AbortCompilation(m_ctx.co);
1.30 - m_ctx.co = NULL;
1.31 + delete m_pRuntime;
1.32 + m_pRuntime = NULL;
1.33 }
1.34
1.35 - if (m_plugin)
1.36 - {
1.37 - g_pSourcePawn->FreeFromMemory(m_plugin);
1.38 - m_plugin = NULL;
1.39 - }
1.40 if (m_pProps)
1.41 {
1.42 sm_trie_destroy(m_pProps);
1.43 @@ -125,7 +110,7 @@
1.44 {
1.45 m_ident = g_ShareSys.CreateIdentity(g_PluginIdent, this);
1.46 m_handle = g_HandleSys.CreateHandle(g_PluginType, this, g_PluginSys.GetIdentity(), g_PluginSys.GetIdentity(), NULL);
1.47 - m_ctx.base->SetIdentity(m_ident);
1.48 + m_pRuntime->GetDefaultContext()->SetKey(1, m_ident);
1.49 }
1.50 }
1.51
1.52 @@ -157,37 +142,6 @@
1.53 base_size += (*i).size();
1.54 }
1.55
1.56 - if (m_plugin != NULL)
1.57 - {
1.58 - base_size += sizeof(sp_plugin_t);
1.59 - base_size += m_plugin->data_size;
1.60 - base_size += m_plugin->pcode_size;
1.61 - base_size += (m_plugin->info.natives_num * sizeof(sp_file_natives_t));
1.62 - base_size += (m_plugin->info.publics_num * sizeof(sp_file_publics_t));
1.63 - base_size += (m_plugin->info.pubvars_num * sizeof(sp_file_pubvars_t));
1.64 - base_size += (m_plugin->debug.files_num * sizeof(sp_fdbg_file_t));
1.65 - base_size += (m_plugin->debug.lines_num * sizeof(sp_fdbg_line_t));
1.66 - base_size += (m_plugin->debug.syms_num * sizeof(sp_fdbg_symbol_t));
1.67 - /* We can't get strtab size, oh well. */
1.68 - }
1.69 -
1.70 - if (m_ctx.base != NULL)
1.71 - {
1.72 - base_size += sizeof(BaseContext);
1.73 - base_size += m_ctx.base->GetPublicsNum() * sizeof(CFunction);
1.74 - }
1.75 - if (m_ctx.ctx != NULL)
1.76 - {
1.77 - base_size += m_ctx.ctx->mem_size;
1.78 - base_size += (m_plugin->debug.files_num * sizeof(sp_debug_file_t));
1.79 - base_size += (m_plugin->debug.lines_num * sizeof(sp_debug_line_t));
1.80 - base_size += (m_plugin->debug.syms_num * sizeof(sp_debug_symbol_t));
1.81 - base_size += (m_plugin->info.pubvars_num * sizeof(sp_pubvar_t));
1.82 - base_size += (m_plugin->info.publics_num * sizeof(sp_public_t));
1.83 - base_size += (m_plugin->info.natives_num * sizeof(sp_native_t));
1.84 - /* We also don't know the JIT code size, oh well. */
1.85 - }
1.86 -
1.87 return base_size;
1.88 }
1.89
1.90 @@ -214,23 +168,6 @@
1.91 return pPlugin;
1.92 }
1.93
1.94 - int err;
1.95 - sp_plugin_t *pl = g_pSourcePawn->LoadFromFilePointer(fp, &err);
1.96 - if (pl == NULL)
1.97 - {
1.98 - fclose(fp);
1.99 - if (error)
1.100 - {
1.101 - snprintf(error, maxlength, "Error %d while parsing plugin", err);
1.102 - }
1.103 - pPlugin->m_status = Plugin_BadLoad;
1.104 - return pPlugin;
1.105 - }
1.106 -
1.107 - fclose(fp);
1.108 -
1.109 - pPlugin->m_plugin = pl;
1.110 -
1.111 return pPlugin;
1.112 }
1.113
1.114 @@ -251,69 +188,9 @@
1.115 return sm_trie_insert(m_pProps, prop, ptr);
1.116 }
1.117
1.118 -ICompilation *CPlugin::StartMyCompile(IVirtualMachine *vm)
1.119 -{
1.120 - if (!m_plugin)
1.121 - {
1.122 - return NULL;
1.123 - }
1.124 -
1.125 - /* :NOTICE: We will eventually need to change these natives
1.126 - * for swapping in new contexts
1.127 - */
1.128 - if (m_ctx.co || m_ctx.ctx)
1.129 - {
1.130 - return NULL;
1.131 - }
1.132 -
1.133 - m_status = Plugin_Uncompiled;
1.134 -
1.135 - m_ctx.vm = vm;
1.136 - m_ctx.co = vm->StartCompilation(m_plugin);
1.137 -
1.138 - return m_ctx.co;
1.139 -}
1.140 -
1.141 -void CPlugin::CancelMyCompile()
1.142 +IPluginRuntime *CPlugin::GetRuntime()
1.143 {
1.144 - if (!m_ctx.co)
1.145 - {
1.146 - return;
1.147 - }
1.148 -
1.149 - m_ctx.vm->AbortCompilation(m_ctx.co);
1.150 - m_ctx.co = NULL;
1.151 - m_ctx.vm = NULL;
1.152 -}
1.153 -
1.154 -bool CPlugin::FinishMyCompile(char *error, size_t maxlength)
1.155 -{
1.156 - if (!m_ctx.co)
1.157 - {
1.158 - return false;
1.159 - }
1.160 -
1.161 - int err;
1.162 - m_ctx.ctx = m_ctx.vm->CompileToContext(m_ctx.co, &err);
1.163 - if (!m_ctx.ctx)
1.164 - {
1.165 - memset(&m_ctx, 0, sizeof(m_ctx));
1.166 - if (error)
1.167 - {
1.168 - snprintf(error, maxlength, "JIT failed to compile (error %d)", err);
1.169 - }
1.170 - return false;
1.171 - }
1.172 -
1.173 - m_ctx.base = new BaseContext(m_ctx.ctx);
1.174 - m_ctx.ctx->user[SM_CONTEXTVAR_MYSELF] = (void *)this;
1.175 -
1.176 - m_status = Plugin_Created;
1.177 - m_ctx.co = NULL;
1.178 -
1.179 - UpdateInfo();
1.180 -
1.181 - return true;
1.182 + return m_pRuntime;
1.183 }
1.184
1.185 void CPlugin::SetErrorState(PluginStatus status, const char *error_fmt, ...)
1.186 @@ -332,9 +209,9 @@
1.187 vsnprintf(m_errormsg, sizeof(m_errormsg), error_fmt, ap);
1.188 va_end(ap);
1.189
1.190 - if (m_ctx.ctx)
1.191 + if (m_pRuntime != NULL)
1.192 {
1.193 - m_ctx.ctx->flags |= SPFLAG_PLUGIN_PAUSED;
1.194 + m_pRuntime->SetPauseState(true);
1.195 }
1.196 }
1.197
1.198 @@ -415,7 +292,7 @@
1.199 m_status = Plugin_Running;
1.200
1.201 cell_t result;
1.202 - IPluginFunction *pFunction = m_ctx.base->GetFunctionByName("OnPluginStart");
1.203 + IPluginFunction *pFunction = m_pRuntime->GetFunctionByName("OnPluginStart");
1.204 if (!pFunction)
1.205 {
1.206 return;
1.207 @@ -436,7 +313,7 @@
1.208 }
1.209
1.210 cell_t result;
1.211 - IPluginFunction *pFunction = m_ctx.base->GetFunctionByName("OnPluginEnd");
1.212 + IPluginFunction *pFunction = m_pRuntime->GetFunctionByName("OnPluginEnd");
1.213 if (!pFunction)
1.214 {
1.215 return;
1.216 @@ -460,7 +337,7 @@
1.217 m_bGotAllLoaded = true;
1.218
1.219 cell_t result;
1.220 - IPluginFunction *pFunction = m_ctx.base->GetFunctionByName("OnAllPluginsLoaded");
1.221 + IPluginFunction *pFunction = m_pRuntime->GetFunctionByName("OnAllPluginsLoaded");
1.222 if (pFunction != NULL)
1.223 {
1.224 pFunction->Execute(&result);
1.225 @@ -468,7 +345,7 @@
1.226
1.227 if (g_OnMapStarted)
1.228 {
1.229 - if ((pFunction = m_ctx.base->GetFunctionByName("OnMapStart")) != NULL)
1.230 + if ((pFunction = m_pRuntime->GetFunctionByName("OnMapStart")) != NULL)
1.231 {
1.232 pFunction->Execute(NULL);
1.233 }
1.234 @@ -491,7 +368,7 @@
1.235
1.236 int err;
1.237 cell_t result;
1.238 - IPluginFunction *pFunction = m_ctx.base->GetFunctionByName("AskPluginLoad");
1.239 + IPluginFunction *pFunction = m_pRuntime->GetFunctionByName("AskPluginLoad");
1.240
1.241 if (!pFunction)
1.242 {
1.243 @@ -515,19 +392,24 @@
1.244 return true;
1.245 }
1.246
1.247 -const sp_plugin_t *CPlugin::GetPluginStructure()
1.248 +void *CPlugin::GetPluginStructure()
1.249 {
1.250 - return m_plugin;
1.251 + return NULL;
1.252 }
1.253
1.254 IPluginContext *CPlugin::GetBaseContext()
1.255 {
1.256 - return m_ctx.base;
1.257 + if (m_pRuntime == NULL)
1.258 + {
1.259 + return NULL;
1.260 + }
1.261 +
1.262 + return m_pRuntime->GetDefaultContext();
1.263 }
1.264
1.265 sp_context_t *CPlugin::GetContext()
1.266 {
1.267 - return m_ctx.ctx;
1.268 + return NULL;
1.269 }
1.270
1.271 const char *CPlugin::GetFilename()
1.272 @@ -557,12 +439,12 @@
1.273
1.274 bool CPlugin::IsDebugging()
1.275 {
1.276 - if (!m_ctx.ctx)
1.277 + if (m_pRuntime == NULL)
1.278 {
1.279 return false;
1.280 }
1.281
1.282 - return ((m_ctx.ctx->flags & SP_FLAG_DEBUG) == SP_FLAG_DEBUG);
1.283 + return m_pRuntime->IsDebugging();
1.284 }
1.285
1.286 void CPlugin::LibraryActions(bool dropping)
1.287 @@ -590,7 +472,7 @@
1.288 LibraryActions(true);
1.289 }
1.290
1.291 - IPluginFunction *pFunction = m_ctx.base->GetFunctionByName("OnPluginPauseChange");
1.292 + IPluginFunction *pFunction = m_pRuntime->GetFunctionByName("OnPluginPauseChange");
1.293 if (pFunction)
1.294 {
1.295 cell_t result;
1.296 @@ -601,10 +483,10 @@
1.297 if (paused)
1.298 {
1.299 m_status = Plugin_Paused;
1.300 - m_ctx.ctx->flags |= SPFLAG_PLUGIN_PAUSED;
1.301 + m_pRuntime->SetPauseState(true);
1.302 } else {
1.303 m_status = Plugin_Running;
1.304 - m_ctx.ctx->flags &= ~SPFLAG_PLUGIN_PAUSED;
1.305 + m_pRuntime->SetPauseState(false);
1.306 }
1.307
1.308 g_PluginSys._SetPauseState(this, paused);
1.309 @@ -652,8 +534,9 @@
1.310 return false;
1.311 }
1.312
1.313 - ICompilation *co = g_pVM->StartCompilation(m_ctx.ctx->plugin);
1.314 - if (!g_pVM->SetCompilationOption(co, "debug", (debug) ? "1" : "0"))
1.315 + ICompilation *co = g_pSourcePawn2->StartCompilation();
1.316 +
1.317 + if (!co->SetOption("debug", (debug) ? "1" : "0"))
1.318 {
1.319 if (error)
1.320 {
1.321 @@ -662,32 +545,7 @@
1.322 return false;
1.323 }
1.324
1.325 - sp_context_t *new_ctx = g_pVM->CompileToContext(co, &err);
1.326 -
1.327 - if (new_ctx)
1.328 - {
1.329 - memcpy(new_ctx->memory, m_ctx.ctx->memory, m_ctx.ctx->mem_size);
1.330 - new_ctx->hp = m_ctx.ctx->hp;
1.331 - new_ctx->sp = m_ctx.ctx->sp;
1.332 - new_ctx->frm = m_ctx.ctx->frm;
1.333 - new_ctx->dbreak = m_ctx.ctx->dbreak;
1.334 - new_ctx->context = m_ctx.ctx->context;
1.335 - memcpy(new_ctx->user, m_ctx.ctx->user, sizeof(m_ctx.ctx->user));
1.336 -
1.337 - uint32_t nativeCount = m_plugin->info.natives_num;
1.338 - for (uint32_t i=0; i<nativeCount; i++)
1.339 - {
1.340 - new_ctx->natives[i].pfn = m_ctx.ctx->natives[i].pfn;
1.341 - new_ctx->natives[i].status = m_ctx.ctx->natives[i].status;
1.342 - }
1.343 -
1.344 - g_pVM->FreeContext(m_ctx.ctx);
1.345 - m_ctx.ctx = new_ctx;
1.346 - m_ctx.base->SetContext(new_ctx);
1.347 -
1.348 - UpdateInfo();
1.349 - }
1.350 - else
1.351 + if ((err = m_pRuntime->ApplyCompilationOptions(co)) != SP_ERROR_NONE)
1.352 {
1.353 if (error)
1.354 {
1.355 @@ -696,6 +554,8 @@
1.356 return false;
1.357 }
1.358
1.359 + UpdateInfo();
1.360 +
1.361 return true;
1.362 }
1.363
1.364 @@ -741,7 +601,7 @@
1.365
1.366 void CPlugin::DependencyDropped(CPlugin *pOwner)
1.367 {
1.368 - if (!m_ctx.ctx)
1.369 + if (!m_pRuntime)
1.370 {
1.371 return;
1.372 }
1.373 @@ -771,12 +631,12 @@
1.374 {
1.375 pNative = (*iter);
1.376 /* Find this native! */
1.377 - if (m_ctx.base->FindNativeByName(pNative->name, &idx) != SP_ERROR_NONE)
1.378 + if (m_pRuntime->FindNativeByName(pNative->name, &idx) != SP_ERROR_NONE)
1.379 {
1.380 continue;
1.381 }
1.382 /* Unbind it */
1.383 - m_ctx.base->GetNativeByIndex(idx, &native);
1.384 + m_pRuntime->GetNativeByIndex(idx, &native);
1.385 native->pfn = NULL;
1.386 native->status = SP_NATIVE_UNBOUND;
1.387 unbound++;
1.388 @@ -1047,6 +907,7 @@
1.389 return LoadRes_NeverLoad;
1.390 }
1.391
1.392 + int err;
1.393 bool no_load = false;
1.394 PluginSettings *pset;
1.395 unsigned int setcount = m_PluginInfo.GetSettingsNum();
1.396 @@ -1102,7 +963,7 @@
1.397
1.398 if (pPlugin->m_status == Plugin_Uncompiled)
1.399 {
1.400 - co = pPlugin->StartMyCompile(g_pVM);
1.401 + co = g_pSourcePawn2->StartCompilation();
1.402 }
1.403
1.404 for (unsigned int i=0; i<setcount; i++)
1.405 @@ -1122,13 +983,13 @@
1.406 {
1.407 continue;
1.408 }
1.409 - if (!g_pVM->SetCompilationOption(co, key, val))
1.410 + if ((err = co->SetOption(key, val)) == SP_ERROR_NONE)
1.411 {
1.412 if (error)
1.413 {
1.414 snprintf(error, maxlength, "Unable to set JIT option (key \"%s\") (value \"%s\")", key, val);
1.415 }
1.416 - pPlugin->CancelMyCompile();
1.417 + co->Abort();
1.418 co = NULL;
1.419 break;
1.420 }
1.421 @@ -1137,10 +998,25 @@
1.422 }
1.423
1.424 /* Do the actual compiling */
1.425 - if (co)
1.426 + if (co != NULL)
1.427 {
1.428 - pPlugin->FinishMyCompile(error, maxlength);
1.429 - co = NULL;
1.430 + char fullpath[PLATFORM_MAX_PATH];
1.431 + g_SourceMod.BuildPath(Path_SM, fullpath, sizeof(fullpath), "plugins/%s", pPlugin->m_filename);
1.432 +
1.433 + pPlugin->m_pRuntime = g_pSourcePawn2->LoadPlugin(co, fullpath, &err);
1.434 + if (pPlugin->m_pRuntime == NULL)
1.435 + {
1.436 + snprintf(error,
1.437 + maxlength,
1.438 + "Unable to load plugin (error %d: %s)",
1.439 + err,
1.440 + g_pSourcePawn2->GetErrorString(err));
1.441 + }
1.442 + else
1.443 + {
1.444 + pPlugin->UpdateInfo();
1.445 + pPlugin->m_status = Plugin_Created;
1.446 + }
1.447 }
1.448
1.449 /* Get the status */
1.450 @@ -1321,7 +1197,7 @@
1.451 {
1.452 cell_t res;
1.453 pFunc->Execute(&res);
1.454 - if (pPlugin->GetContext()->n_err != SP_ERROR_NONE)
1.455 + if (pPlugin->GetBaseContext()->GetLastNativeError() != SP_ERROR_NONE)
1.456 {
1.457 if (error)
1.458 {
1.459 @@ -1448,7 +1324,7 @@
1.460 {
1.461 cell_t res;
1.462 pFunc->Execute(&res);
1.463 - if (pPlugin->GetContext()->n_err != SP_ERROR_NONE)
1.464 + if (pPlugin->GetBaseContext()->GetLastNativeError() != SP_ERROR_NONE)
1.465 {
1.466 if (error)
1.467 {
1.468 @@ -1615,9 +1491,9 @@
1.469 {
1.470 /* If we got here, all natives are okay again! */
1.471 pPlugin->m_status = Plugin_Running;
1.472 - if ((pPlugin->m_ctx.ctx->flags & SPFLAG_PLUGIN_PAUSED) == SPFLAG_PLUGIN_PAUSED)
1.473 + if (pPlugin->m_pRuntime->IsPaused())
1.474 {
1.475 - pPlugin->m_ctx.ctx->flags &= ~SPFLAG_PLUGIN_PAUSED;
1.476 + pPlugin->m_pRuntime->SetPauseState(false);
1.477 _SetPauseState(pPlugin, false);
1.478 }
1.479 }
1.480 @@ -1687,8 +1563,24 @@
1.481
1.482 IPlugin *CPluginManager::FindPluginByContext(const sp_context_t *ctx)
1.483 {
1.484 - IPlugin *pl = (IPlugin *)ctx->user[SM_CONTEXTVAR_MYSELF];
1.485 - return pl;
1.486 + List<CPlugin *>::iterator iter;
1.487 +
1.488 + /* :TODO: :TODO: respeed this up somehow */
1.489 + for (iter = m_plugins.begin(); iter != m_plugins.end(); iter++)
1.490 + {
1.491 + CPlugin *pl = (*iter);
1.492 + if (pl->m_pRuntime != NULL && pl->m_pRuntime->GetDefaultContext()->GetContext() == ctx)
1.493 + {
1.494 + return pl;
1.495 + }
1.496 + }
1.497 +
1.498 + return NULL;
1.499 +}
1.500 +
1.501 +CPlugin *CPluginManager::GetPluginByCtx(const sp_context_t *ctx)
1.502 +{
1.503 + return (CPlugin *)FindPluginByContext(ctx);
1.504 }
1.505
1.506 unsigned int CPluginManager::GetPluginCount()