OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "../../include/jsapi/fxjs_v8.h" | 7 #include "../../include/jsapi/fxjs_v8.h" |
8 | 8 |
9 #include "../../../core/include/fxcrt/fx_basic.h" | 9 #include "../../../core/include/fxcrt/fx_basic.h" |
10 | 10 |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 v8::Isolate::Scope isolate_scope(pIsolate); | 266 v8::Isolate::Scope isolate_scope(pIsolate); |
267 v8::HandleScope handle_scope(pIsolate); | 267 v8::HandleScope handle_scope(pIsolate); |
268 CFX_ByteString bsConst = CFX_WideString(sConstName).UTF8Encode(); | 268 CFX_ByteString bsConst = CFX_WideString(sConstName).UTF8Encode(); |
269 GetGlobalObjectTemplate(pIsolate)->Set( | 269 GetGlobalObjectTemplate(pIsolate)->Set( |
270 v8::String::NewFromUtf8(pIsolate, bsConst.c_str(), | 270 v8::String::NewFromUtf8(pIsolate, bsConst.c_str(), |
271 v8::NewStringType::kNormal).ToLocalChecked(), | 271 v8::NewStringType::kNormal).ToLocalChecked(), |
272 pDefault, v8::ReadOnly); | 272 pDefault, v8::ReadOnly); |
273 } | 273 } |
274 | 274 |
275 void FXJS_InitializeRuntime(v8::Isolate* pIsolate, | 275 void FXJS_InitializeRuntime(v8::Isolate* pIsolate, |
276 IJS_Runtime* pFXRuntime, | 276 IJS_Runtime* pIRuntime, |
277 IJS_Context* context, | |
278 v8::Global<v8::Context>& v8PersistentContext) { | 277 v8::Global<v8::Context>& v8PersistentContext) { |
279 if (pIsolate == g_isolate) | 278 if (pIsolate == g_isolate) |
280 ++g_isolate_ref_count; | 279 ++g_isolate_ref_count; |
281 | 280 |
282 v8::Isolate::Scope isolate_scope(pIsolate); | 281 v8::Isolate::Scope isolate_scope(pIsolate); |
283 v8::HandleScope handle_scope(pIsolate); | 282 v8::HandleScope handle_scope(pIsolate); |
284 v8::Local<v8::Context> v8Context = | 283 v8::Local<v8::Context> v8Context = |
285 v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate)); | 284 v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate)); |
286 v8::Context::Scope context_scope(v8Context); | 285 v8::Context::Scope context_scope(v8Context); |
287 | 286 |
288 FXJS_PerIsolateData::SetUp(pIsolate); | 287 FXJS_PerIsolateData::SetUp(pIsolate); |
289 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pFXRuntime); | 288 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pIRuntime); |
290 | 289 |
291 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | 290 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
292 for (int i = 0; i < maxID; ++i) { | 291 for (int i = 0; i < maxID; ++i) { |
293 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | 292 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
294 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); | 293 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); |
295 v8::Local<v8::String> m_ObjName = | 294 v8::Local<v8::String> m_ObjName = |
296 v8::String::NewFromUtf8(pIsolate, bs.c_str(), | 295 v8::String::NewFromUtf8(pIsolate, bs.c_str(), |
297 v8::NewStringType::kNormal, | 296 v8::NewStringType::kNormal, |
298 bs.GetLength()).ToLocalChecked(); | 297 bs.GetLength()).ToLocalChecked(); |
299 | 298 |
300 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { | 299 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { |
301 v8Context->Global() | 300 v8Context->Global() |
302 ->GetPrototype() | 301 ->GetPrototype() |
303 ->ToObject(v8Context) | 302 ->ToObject(v8Context) |
304 .ToLocalChecked() | 303 .ToLocalChecked() |
305 ->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(i)); | 304 ->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(i)); |
306 | 305 |
307 if (pObjDef->m_pConstructor) | 306 if (pObjDef->m_pConstructor) |
308 pObjDef->m_pConstructor(context, v8Context->Global() | 307 pObjDef->m_pConstructor(pIRuntime, v8Context->Global() |
309 ->GetPrototype() | 308 ->GetPrototype() |
310 ->ToObject(v8Context) | 309 ->ToObject(v8Context) |
311 .ToLocalChecked()); | 310 .ToLocalChecked()); |
312 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { | 311 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { |
313 v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, context, i); | 312 v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, pIRuntime, i); |
314 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); | 313 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); |
315 pObjDef->m_StaticObj.Reset(pIsolate, obj); | 314 pObjDef->m_StaticObj.Reset(pIsolate, obj); |
316 } | 315 } |
317 } | 316 } |
318 v8PersistentContext.Reset(pIsolate, v8Context); | 317 v8PersistentContext.Reset(pIsolate, v8Context); |
319 } | 318 } |
320 | 319 |
321 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, | 320 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, |
322 v8::Global<v8::Context>& v8PersistentContext) { | 321 v8::Global<v8::Context>& v8PersistentContext) { |
323 if (pIsolate == g_isolate && --g_isolate_ref_count > 0) | 322 if (pIsolate == g_isolate && --g_isolate_ref_count > 0) |
(...skipping 28 matching lines...) Expand all Loading... |
352 | 351 |
353 IJS_Runtime* FXJS_GetRuntimeFromIsolate(v8::Isolate* pIsolate) { | 352 IJS_Runtime* FXJS_GetRuntimeFromIsolate(v8::Isolate* pIsolate) { |
354 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 353 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
355 return static_cast<IJS_Runtime*>( | 354 return static_cast<IJS_Runtime*>( |
356 context->GetAlignedPointerFromEmbedderData(kPerContextDataIndex)); | 355 context->GetAlignedPointerFromEmbedderData(kPerContextDataIndex)); |
357 } | 356 } |
358 | 357 |
359 int FXJS_Execute(v8::Isolate* pIsolate, | 358 int FXJS_Execute(v8::Isolate* pIsolate, |
360 IJS_Context* pJSContext, | 359 IJS_Context* pJSContext, |
361 const wchar_t* script, | 360 const wchar_t* script, |
362 long length, | |
363 FXJSErr* pError) { | 361 FXJSErr* pError) { |
364 v8::Isolate::Scope isolate_scope(pIsolate); | 362 v8::Isolate::Scope isolate_scope(pIsolate); |
365 v8::TryCatch try_catch(pIsolate); | 363 v8::TryCatch try_catch(pIsolate); |
366 CFX_ByteString bsScript = CFX_WideString(script).UTF8Encode(); | 364 CFX_ByteString bsScript = CFX_WideString(script).UTF8Encode(); |
367 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 365 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
368 v8::Local<v8::Script> compiled_script; | 366 v8::Local<v8::Script> compiled_script; |
369 if (!v8::Script::Compile( | 367 if (!v8::Script::Compile( |
370 context, v8::String::NewFromUtf8( | 368 context, v8::String::NewFromUtf8( |
371 pIsolate, bsScript.c_str(), v8::NewStringType::kNormal, | 369 pIsolate, bsScript.c_str(), v8::NewStringType::kNormal, |
372 bsScript.GetLength()).ToLocalChecked()) | 370 bsScript.GetLength()).ToLocalChecked()) |
373 .ToLocal(&compiled_script)) { | 371 .ToLocal(&compiled_script)) { |
374 v8::String::Utf8Value error(try_catch.Exception()); | 372 v8::String::Utf8Value error(try_catch.Exception()); |
375 // TODO(tsepez): return error via pError->message. | 373 // TODO(tsepez): return error via pError->message. |
376 return -1; | 374 return -1; |
377 } | 375 } |
378 | 376 |
379 v8::Local<v8::Value> result; | 377 v8::Local<v8::Value> result; |
380 if (!compiled_script->Run(context).ToLocal(&result)) { | 378 if (!compiled_script->Run(context).ToLocal(&result)) { |
381 v8::String::Utf8Value error(try_catch.Exception()); | 379 v8::String::Utf8Value error(try_catch.Exception()); |
382 // TODO(tsepez): return error via pError->message. | 380 // TODO(tsepez): return error via pError->message. |
383 return -1; | 381 return -1; |
384 } | 382 } |
385 return 0; | 383 return 0; |
386 } | 384 } |
387 | 385 |
388 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, | 386 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, |
389 IJS_Context* pJSContext, | 387 IJS_Runtime* pIRuntime, |
390 int nObjDefnID) { | 388 int nObjDefnID) { |
391 v8::Isolate::Scope isolate_scope(pIsolate); | 389 v8::Isolate::Scope isolate_scope(pIsolate); |
392 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 390 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
393 if (nObjDefnID == -1) { | 391 if (nObjDefnID == -1) { |
394 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate); | 392 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate); |
395 v8::Local<v8::Object> obj; | 393 v8::Local<v8::Object> obj; |
396 if (!objTempl->NewInstance(context).ToLocal(&obj)) | 394 if (!objTempl->NewInstance(context).ToLocal(&obj)) |
397 return v8::Local<v8::Object>(); | 395 return v8::Local<v8::Object>(); |
398 return obj; | 396 return obj; |
399 } | 397 } |
400 | 398 |
401 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); | 399 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); |
402 if (!pData) | 400 if (!pData) |
403 return v8::Local<v8::Object>(); | 401 return v8::Local<v8::Object>(); |
404 | 402 |
405 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate)) | 403 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate)) |
406 return v8::Local<v8::Object>(); | 404 return v8::Local<v8::Object>(); |
407 | 405 |
408 CFXJS_ObjDefinition* pObjDef = | 406 CFXJS_ObjDefinition* pObjDef = |
409 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); | 407 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); |
410 v8::Local<v8::Object> obj; | 408 v8::Local<v8::Object> obj; |
411 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj)) | 409 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj)) |
412 return v8::Local<v8::Object>(); | 410 return v8::Local<v8::Object>(); |
413 | 411 |
414 obj->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(nObjDefnID)); | 412 obj->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(nObjDefnID)); |
415 if (pObjDef->m_pConstructor) | 413 if (pObjDef->m_pConstructor) |
416 pObjDef->m_pConstructor(pJSContext, obj); | 414 pObjDef->m_pConstructor(pIRuntime, obj); |
417 | 415 |
418 return obj; | 416 return obj; |
419 } | 417 } |
420 | 418 |
421 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate) { | 419 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate) { |
422 v8::Isolate::Scope isolate_scope(pIsolate); | 420 v8::Isolate::Scope isolate_scope(pIsolate); |
423 if (!FXJS_PerIsolateData::Get(pIsolate)) | 421 if (!FXJS_PerIsolateData::Get(pIsolate)) |
424 return v8::Local<v8::Object>(); | 422 return v8::Local<v8::Object>(); |
425 | 423 |
426 // Return the global object. | 424 // Return the global object. |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
748 return v8::Local<v8::Array>(); | 746 return v8::Local<v8::Array>(); |
749 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 747 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
750 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); | 748 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); |
751 } | 749 } |
752 | 750 |
753 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { | 751 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { |
754 pTo = pFrom; | 752 pTo = pFrom; |
755 } | 753 } |
756 | 754 |
757 | 755 |
OLD | NEW |