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 "fxjse/include/cfxjse_value.h" | 7 #include "fxjs/include/cfxjse_value.h" |
8 | 8 |
9 #include <math.h> | 9 #include <math.h> |
10 | 10 |
11 #include "fxjse/context.h" | 11 #include "fxjs/include/cfxjse_class.h" |
12 #include "fxjse/include/cfxjse_class.h" | 12 #include "fxjs/include/cfxjse_context.h" |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 double FXJSE_ftod(FX_FLOAT fNumber) { | 16 double ftod(FX_FLOAT fNumber) { |
17 static_assert(sizeof(FX_FLOAT) == 4, "FX_FLOAT of incorrect size"); | 17 static_assert(sizeof(FX_FLOAT) == 4, "FX_FLOAT of incorrect size"); |
18 | 18 |
19 uint32_t nFloatBits = (uint32_t&)fNumber; | 19 uint32_t nFloatBits = (uint32_t&)fNumber; |
20 uint8_t nExponent = (uint8_t)(nFloatBits >> 23); | 20 uint8_t nExponent = (uint8_t)(nFloatBits >> 23); |
21 if (nExponent == 0 || nExponent == 255) | 21 if (nExponent == 0 || nExponent == 255) |
22 return fNumber; | 22 return fNumber; |
23 | 23 |
24 int8_t nErrExp = nExponent - 150; | 24 int8_t nErrExp = nExponent - 150; |
25 if (nErrExp >= 0) | 25 if (nErrExp >= 0) |
26 return fNumber; | 26 return fNumber; |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 } | 117 } |
118 | 118 |
119 void CFXJSE_Value::SetDate(double dDouble) { | 119 void CFXJSE_Value::SetDate(double dDouble) { |
120 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); | 120 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); |
121 v8::Local<v8::Value> hDate = v8::Date::New(m_pIsolate, dDouble); | 121 v8::Local<v8::Value> hDate = v8::Date::New(m_pIsolate, dDouble); |
122 m_hValue.Reset(m_pIsolate, hDate); | 122 m_hValue.Reset(m_pIsolate, hDate); |
123 } | 123 } |
124 | 124 |
125 void CFXJSE_Value::SetFloat(FX_FLOAT fFloat) { | 125 void CFXJSE_Value::SetFloat(FX_FLOAT fFloat) { |
126 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); | 126 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); |
127 v8::Local<v8::Value> pValue = v8::Number::New(m_pIsolate, FXJSE_ftod(fFloat)); | 127 v8::Local<v8::Value> pValue = v8::Number::New(m_pIsolate, ftod(fFloat)); |
128 m_hValue.Reset(m_pIsolate, pValue); | 128 m_hValue.Reset(m_pIsolate, pValue); |
129 } | 129 } |
130 | 130 |
131 FX_BOOL CFXJSE_Value::SetObjectProperty(const CFX_ByteStringC& szPropName, | 131 FX_BOOL CFXJSE_Value::SetObjectProperty(const CFX_ByteStringC& szPropName, |
132 CFXJSE_Value* lpPropValue) { | 132 CFXJSE_Value* lpPropValue) { |
133 ASSERT(lpPropValue); | 133 ASSERT(lpPropValue); |
134 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); | 134 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); |
135 v8::Local<v8::Value> hObject = | 135 v8::Local<v8::Value> hObject = |
136 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | 136 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
137 if (!hObject->IsObject()) | 137 if (!hObject->IsObject()) |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 if (lpRetValue) | 339 if (lpRetValue) |
340 lpRetValue->ForceSetValue(hReturnValue); | 340 lpRetValue->ForceSetValue(hReturnValue); |
341 | 341 |
342 if (lpLocalArgs) { | 342 if (lpLocalArgs) { |
343 for (uint32_t i = 0; i < nArgCount; i++) | 343 for (uint32_t i = 0; i < nArgCount; i++) |
344 lpLocalArgs[i].~Local(); | 344 lpLocalArgs[i].~Local(); |
345 FX_Free(lpLocalArgs); | 345 FX_Free(lpLocalArgs); |
346 } | 346 } |
347 return bRetValue; | 347 return bRetValue; |
348 } | 348 } |
| 349 |
| 350 FX_BOOL CFXJSE_Value::IsUndefined() const { |
| 351 if (m_hValue.IsEmpty()) |
| 352 return FALSE; |
| 353 |
| 354 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); |
| 355 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 356 return hValue->IsUndefined(); |
| 357 } |
| 358 |
| 359 FX_BOOL CFXJSE_Value::IsNull() const { |
| 360 if (m_hValue.IsEmpty()) |
| 361 return FALSE; |
| 362 |
| 363 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); |
| 364 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 365 return hValue->IsNull(); |
| 366 } |
| 367 |
| 368 FX_BOOL CFXJSE_Value::IsBoolean() const { |
| 369 if (m_hValue.IsEmpty()) |
| 370 return FALSE; |
| 371 |
| 372 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); |
| 373 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 374 return hValue->IsBoolean(); |
| 375 } |
| 376 |
| 377 FX_BOOL CFXJSE_Value::IsString() const { |
| 378 if (m_hValue.IsEmpty()) |
| 379 return FALSE; |
| 380 |
| 381 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); |
| 382 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 383 return hValue->IsString(); |
| 384 } |
| 385 |
| 386 FX_BOOL CFXJSE_Value::IsNumber() const { |
| 387 if (m_hValue.IsEmpty()) |
| 388 return FALSE; |
| 389 |
| 390 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); |
| 391 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 392 return hValue->IsNumber(); |
| 393 } |
| 394 |
| 395 FX_BOOL CFXJSE_Value::IsInteger() const { |
| 396 if (m_hValue.IsEmpty()) |
| 397 return FALSE; |
| 398 |
| 399 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); |
| 400 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 401 return hValue->IsInt32(); |
| 402 } |
| 403 |
| 404 FX_BOOL CFXJSE_Value::IsObject() const { |
| 405 if (m_hValue.IsEmpty()) |
| 406 return FALSE; |
| 407 |
| 408 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); |
| 409 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 410 return hValue->IsObject(); |
| 411 } |
| 412 |
| 413 FX_BOOL CFXJSE_Value::IsArray() const { |
| 414 if (m_hValue.IsEmpty()) |
| 415 return FALSE; |
| 416 |
| 417 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); |
| 418 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 419 return hValue->IsArray(); |
| 420 } |
| 421 |
| 422 FX_BOOL CFXJSE_Value::IsFunction() const { |
| 423 if (m_hValue.IsEmpty()) |
| 424 return FALSE; |
| 425 |
| 426 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); |
| 427 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 428 return hValue->IsFunction(); |
| 429 } |
| 430 |
| 431 FX_BOOL CFXJSE_Value::IsDate() const { |
| 432 if (m_hValue.IsEmpty()) |
| 433 return FALSE; |
| 434 |
| 435 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); |
| 436 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 437 return hValue->IsDate(); |
| 438 } |
| 439 |
| 440 FX_BOOL CFXJSE_Value::ToBoolean() const { |
| 441 ASSERT(!m_hValue.IsEmpty()); |
| 442 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); |
| 443 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 444 return static_cast<FX_BOOL>(hValue->BooleanValue()); |
| 445 } |
| 446 |
| 447 FX_FLOAT CFXJSE_Value::ToFloat() const { |
| 448 ASSERT(!m_hValue.IsEmpty()); |
| 449 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); |
| 450 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 451 return static_cast<FX_FLOAT>(hValue->NumberValue()); |
| 452 } |
| 453 |
| 454 double CFXJSE_Value::ToDouble() const { |
| 455 ASSERT(!m_hValue.IsEmpty()); |
| 456 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); |
| 457 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 458 return static_cast<double>(hValue->NumberValue()); |
| 459 } |
| 460 |
| 461 int32_t CFXJSE_Value::ToInteger() const { |
| 462 ASSERT(!m_hValue.IsEmpty()); |
| 463 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); |
| 464 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 465 return static_cast<int32_t>(hValue->NumberValue()); |
| 466 } |
| 467 |
| 468 CFX_ByteString CFXJSE_Value::ToString() const { |
| 469 ASSERT(!m_hValue.IsEmpty()); |
| 470 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); |
| 471 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue); |
| 472 v8::Local<v8::String> hString = hValue->ToString(); |
| 473 v8::String::Utf8Value hStringVal(hString); |
| 474 return CFX_ByteString(*hStringVal); |
| 475 } |
| 476 |
| 477 void CFXJSE_Value::SetUndefined() { |
| 478 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); |
| 479 v8::Local<v8::Value> hValue = v8::Undefined(m_pIsolate); |
| 480 m_hValue.Reset(m_pIsolate, hValue); |
| 481 } |
| 482 |
| 483 void CFXJSE_Value::SetNull() { |
| 484 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); |
| 485 v8::Local<v8::Value> hValue = v8::Null(m_pIsolate); |
| 486 m_hValue.Reset(m_pIsolate, hValue); |
| 487 } |
| 488 |
| 489 void CFXJSE_Value::SetBoolean(FX_BOOL bBoolean) { |
| 490 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); |
| 491 v8::Local<v8::Value> hValue = v8::Boolean::New(m_pIsolate, bBoolean != FALSE); |
| 492 m_hValue.Reset(m_pIsolate, hValue); |
| 493 } |
| 494 |
| 495 void CFXJSE_Value::SetInteger(int32_t nInteger) { |
| 496 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); |
| 497 v8::Local<v8::Value> hValue = v8::Integer::New(m_pIsolate, nInteger); |
| 498 m_hValue.Reset(m_pIsolate, hValue); |
| 499 } |
| 500 |
| 501 void CFXJSE_Value::SetDouble(double dDouble) { |
| 502 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); |
| 503 v8::Local<v8::Value> hValue = v8::Number::New(m_pIsolate, dDouble); |
| 504 m_hValue.Reset(m_pIsolate, hValue); |
| 505 } |
| 506 |
| 507 void CFXJSE_Value::SetString(const CFX_ByteStringC& szString) { |
| 508 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); |
| 509 v8::Local<v8::Value> hValue = v8::String::NewFromUtf8( |
| 510 m_pIsolate, reinterpret_cast<const char*>(szString.raw_str()), |
| 511 v8::String::kNormalString, szString.GetLength()); |
| 512 m_hValue.Reset(m_pIsolate, hValue); |
| 513 } |
| 514 |
| 515 void CFXJSE_Value::SetJSObject() { |
| 516 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); |
| 517 v8::Local<v8::Value> hValue = v8::Object::New(m_pIsolate); |
| 518 m_hValue.Reset(m_pIsolate, hValue); |
| 519 } |
OLD | NEW |