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::Locker locker(pIsolate); | 282 v8::Locker locker(pIsolate); |
284 v8::HandleScope handle_scope(pIsolate); | 283 v8::HandleScope handle_scope(pIsolate); |
285 v8::Local<v8::Context> v8Context = | 284 v8::Local<v8::Context> v8Context = |
286 v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate)); | 285 v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate)); |
287 v8::Context::Scope context_scope(v8Context); | 286 v8::Context::Scope context_scope(v8Context); |
288 | 287 |
289 FXJS_PerIsolateData::SetUp(pIsolate); | 288 FXJS_PerIsolateData::SetUp(pIsolate); |
290 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pFXRuntime); | 289 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pIRuntime); |
291 | 290 |
292 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | 291 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
293 for (int i = 0; i < maxID; ++i) { | 292 for (int i = 0; i < maxID; ++i) { |
294 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | 293 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
295 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); | 294 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); |
296 v8::Local<v8::String> m_ObjName = | 295 v8::Local<v8::String> m_ObjName = |
297 v8::String::NewFromUtf8(pIsolate, bs.c_str(), | 296 v8::String::NewFromUtf8(pIsolate, bs.c_str(), |
298 v8::NewStringType::kNormal, | 297 v8::NewStringType::kNormal, |
299 bs.GetLength()).ToLocalChecked(); | 298 bs.GetLength()).ToLocalChecked(); |
300 | 299 |
301 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { | 300 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { |
302 v8Context->Global() | 301 v8Context->Global() |
303 ->GetPrototype() | 302 ->GetPrototype() |
304 ->ToObject(v8Context) | 303 ->ToObject(v8Context) |
305 .ToLocalChecked() | 304 .ToLocalChecked() |
306 ->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(i)); | 305 ->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(i)); |
307 | 306 |
308 if (pObjDef->m_pConstructor) { | 307 if (pObjDef->m_pConstructor) |
309 pObjDef->m_pConstructor(context, v8Context->Global() | 308 pObjDef->m_pConstructor(pIRuntime, v8Context->Global() |
310 ->GetPrototype() | 309 ->GetPrototype() |
311 ->ToObject(v8Context) | 310 ->ToObject(v8Context) |
312 .ToLocalChecked()); | 311 .ToLocalChecked()); |
313 } | |
314 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { | 312 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { |
315 v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, context, i); | 313 v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, pIRuntime, i); |
316 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); | 314 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); |
317 pObjDef->m_StaticObj.Reset(pIsolate, obj); | 315 pObjDef->m_StaticObj.Reset(pIsolate, obj); |
318 } | 316 } |
319 } | 317 } |
320 v8PersistentContext.Reset(pIsolate, v8Context); | 318 v8PersistentContext.Reset(pIsolate, v8Context); |
321 } | 319 } |
322 | 320 |
323 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, | 321 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, |
324 v8::Global<v8::Context>& v8PersistentContext) { | 322 v8::Global<v8::Context>& v8PersistentContext) { |
325 if (pIsolate == g_isolate && --g_isolate_ref_count > 0) | 323 if (pIsolate == g_isolate && --g_isolate_ref_count > 0) |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 | 356 |
359 IJS_Runtime* FXJS_GetRuntimeFromIsolate(v8::Isolate* pIsolate) { | 357 IJS_Runtime* FXJS_GetRuntimeFromIsolate(v8::Isolate* pIsolate) { |
360 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 358 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
361 return static_cast<IJS_Runtime*>( | 359 return static_cast<IJS_Runtime*>( |
362 context->GetAlignedPointerFromEmbedderData(kPerContextDataIndex)); | 360 context->GetAlignedPointerFromEmbedderData(kPerContextDataIndex)); |
363 } | 361 } |
364 | 362 |
365 int FXJS_Execute(v8::Isolate* pIsolate, | 363 int FXJS_Execute(v8::Isolate* pIsolate, |
366 IJS_Context* pJSContext, | 364 IJS_Context* pJSContext, |
367 const wchar_t* script, | 365 const wchar_t* script, |
368 long length, | |
369 FXJSErr* pError) { | 366 FXJSErr* pError) { |
370 v8::Isolate::Scope isolate_scope(pIsolate); | 367 v8::Isolate::Scope isolate_scope(pIsolate); |
371 v8::TryCatch try_catch(pIsolate); | 368 v8::TryCatch try_catch(pIsolate); |
372 CFX_ByteString bsScript = CFX_WideString(script).UTF8Encode(); | 369 CFX_ByteString bsScript = CFX_WideString(script).UTF8Encode(); |
373 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 370 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
374 v8::Local<v8::Script> compiled_script; | 371 v8::Local<v8::Script> compiled_script; |
375 if (!v8::Script::Compile( | 372 if (!v8::Script::Compile( |
376 context, v8::String::NewFromUtf8( | 373 context, v8::String::NewFromUtf8( |
377 pIsolate, bsScript.c_str(), v8::NewStringType::kNormal, | 374 pIsolate, bsScript.c_str(), v8::NewStringType::kNormal, |
378 bsScript.GetLength()).ToLocalChecked()) | 375 bsScript.GetLength()).ToLocalChecked()) |
379 .ToLocal(&compiled_script)) { | 376 .ToLocal(&compiled_script)) { |
380 v8::String::Utf8Value error(try_catch.Exception()); | 377 v8::String::Utf8Value error(try_catch.Exception()); |
381 // TODO(tsepez): return error via pError->message. | 378 // TODO(tsepez): return error via pError->message. |
382 return -1; | 379 return -1; |
383 } | 380 } |
384 | 381 |
385 v8::Local<v8::Value> result; | 382 v8::Local<v8::Value> result; |
386 if (!compiled_script->Run(context).ToLocal(&result)) { | 383 if (!compiled_script->Run(context).ToLocal(&result)) { |
387 v8::String::Utf8Value error(try_catch.Exception()); | 384 v8::String::Utf8Value error(try_catch.Exception()); |
388 // TODO(tsepez): return error via pError->message. | 385 // TODO(tsepez): return error via pError->message. |
389 return -1; | 386 return -1; |
390 } | 387 } |
391 return 0; | 388 return 0; |
392 } | 389 } |
393 | 390 |
394 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, | 391 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, |
395 IJS_Context* pJSContext, | 392 IJS_Runtime* pIRuntime, |
396 int nObjDefnID) { | 393 int nObjDefnID) { |
397 v8::Isolate::Scope isolate_scope(pIsolate); | 394 v8::Isolate::Scope isolate_scope(pIsolate); |
398 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 395 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
399 if (nObjDefnID == -1) { | 396 if (nObjDefnID == -1) { |
400 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate); | 397 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate); |
401 v8::Local<v8::Object> obj; | 398 v8::Local<v8::Object> obj; |
402 if (!objTempl->NewInstance(context).ToLocal(&obj)) | 399 if (!objTempl->NewInstance(context).ToLocal(&obj)) |
403 return v8::Local<v8::Object>(); | 400 return v8::Local<v8::Object>(); |
404 return obj; | 401 return obj; |
405 } | 402 } |
406 | 403 |
407 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); | 404 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); |
408 if (!pData) | 405 if (!pData) |
409 return v8::Local<v8::Object>(); | 406 return v8::Local<v8::Object>(); |
410 | 407 |
411 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate)) | 408 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate)) |
412 return v8::Local<v8::Object>(); | 409 return v8::Local<v8::Object>(); |
413 | 410 |
414 CFXJS_ObjDefinition* pObjDef = | 411 CFXJS_ObjDefinition* pObjDef = |
415 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); | 412 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); |
416 v8::Local<v8::Object> obj; | 413 v8::Local<v8::Object> obj; |
417 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj)) | 414 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj)) |
418 return v8::Local<v8::Object>(); | 415 return v8::Local<v8::Object>(); |
419 | 416 |
420 obj->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(nObjDefnID)); | 417 obj->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(nObjDefnID)); |
421 if (pObjDef->m_pConstructor) | 418 if (pObjDef->m_pConstructor) |
422 pObjDef->m_pConstructor(pJSContext, obj); | 419 pObjDef->m_pConstructor(pIRuntime, obj); |
423 | 420 |
424 return obj; | 421 return obj; |
425 } | 422 } |
426 | 423 |
427 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate) { | 424 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate) { |
428 v8::Isolate::Scope isolate_scope(pIsolate); | 425 v8::Isolate::Scope isolate_scope(pIsolate); |
429 if (!FXJS_PerIsolateData::Get(pIsolate)) | 426 if (!FXJS_PerIsolateData::Get(pIsolate)) |
430 return v8::Local<v8::Object>(); | 427 return v8::Local<v8::Object>(); |
431 | 428 |
432 // Return the global object. | 429 // Return the global object. |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
754 return v8::Local<v8::Array>(); | 751 return v8::Local<v8::Array>(); |
755 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 752 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
756 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); | 753 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); |
757 } | 754 } |
758 | 755 |
759 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { | 756 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { |
760 pTo = pFrom; | 757 pTo = pFrom; |
761 } | 758 } |
762 | 759 |
763 | 760 |
OLD | NEW |