| OLD | NEW |
| 1 // Copyright (c) 2014, the Dartino project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dartino project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
| 4 | 4 |
| 5 #include "src/vm/natives.h" | 5 #include "src/vm/natives.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <math.h> | 8 #include <math.h> |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 | 10 |
| 11 #include "src/shared/bytecodes.h" | 11 #include "src/shared/bytecodes.h" |
| 12 #include "src/shared/flags.h" | 12 #include "src/shared/flags.h" |
| 13 #include "src/shared/names.h" | 13 #include "src/shared/names.h" |
| 14 #include "src/shared/selectors.h" | 14 #include "src/shared/selectors.h" |
| 15 #include "src/shared/platform.h" | 15 #include "src/shared/platform.h" |
| 16 | 16 |
| 17 #include "src/vm/event_handler.h" | 17 #include "src/vm/event_handler.h" |
| 18 #include "src/vm/interpreter.h" | 18 #include "src/vm/interpreter.h" |
| 19 #include "src/vm/native_interpreter.h" | 19 #include "src/vm/native_interpreter.h" |
| 20 #include "src/vm/port.h" | 20 #include "src/vm/port.h" |
| 21 #include "src/vm/process.h" | 21 #include "src/vm/process.h" |
| 22 #include "src/vm/scheduler.h" | 22 #include "src/vm/scheduler.h" |
| 23 #include "src/vm/session.h" | 23 #include "src/vm/session.h" |
| 24 | 24 |
| 25 #include "third_party/double-conversion/src/double-conversion.h" | 25 #include "third_party/double-conversion/src/double-conversion.h" |
| 26 | 26 |
| 27 namespace fletch { | 27 namespace dartino { |
| 28 | 28 |
| 29 static const char kDoubleExponentChar = 'e'; | 29 static const char kDoubleExponentChar = 'e'; |
| 30 static const char* kDoubleInfinitySymbol = "Infinity"; | 30 static const char* kDoubleInfinitySymbol = "Infinity"; |
| 31 static const char* kDoubleNaNSymbol = "NaN"; | 31 static const char* kDoubleNaNSymbol = "NaN"; |
| 32 | 32 |
| 33 static Object* ToBool(Process* process, bool value) { | 33 static Object* ToBool(Process* process, bool value) { |
| 34 Program* program = process->program(); | 34 Program* program = process->program(); |
| 35 return value ? program->true_object() : program->false_object(); | 35 return value ? program->true_object() : program->false_object(); |
| 36 } | 36 } |
| 37 | 37 |
| (...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 BEGIN_NATIVE(DoubleNegate) { | 514 BEGIN_NATIVE(DoubleNegate) { |
| 515 Double* x = Double::cast(arguments[0]); | 515 Double* x = Double::cast(arguments[0]); |
| 516 return process->NewDouble(-x->value()); | 516 return process->NewDouble(-x->value()); |
| 517 } | 517 } |
| 518 END_NATIVE() | 518 END_NATIVE() |
| 519 | 519 |
| 520 BEGIN_NATIVE(DoubleAdd) { | 520 BEGIN_NATIVE(DoubleAdd) { |
| 521 Double* x = Double::cast(arguments[0]); | 521 Double* x = Double::cast(arguments[0]); |
| 522 Object* y = arguments[1]; | 522 Object* y = arguments[1]; |
| 523 if (!y->IsDouble()) return Failure::wrong_argument_type(); | 523 if (!y->IsDouble()) return Failure::wrong_argument_type(); |
| 524 fletch_double y_value = Double::cast(y)->value(); | 524 dartino_double y_value = Double::cast(y)->value(); |
| 525 return process->NewDouble(x->value() + y_value); | 525 return process->NewDouble(x->value() + y_value); |
| 526 } | 526 } |
| 527 END_NATIVE() | 527 END_NATIVE() |
| 528 | 528 |
| 529 BEGIN_NATIVE(DoubleSub) { | 529 BEGIN_NATIVE(DoubleSub) { |
| 530 Double* x = Double::cast(arguments[0]); | 530 Double* x = Double::cast(arguments[0]); |
| 531 Object* y = arguments[1]; | 531 Object* y = arguments[1]; |
| 532 if (!y->IsDouble()) return Failure::wrong_argument_type(); | 532 if (!y->IsDouble()) return Failure::wrong_argument_type(); |
| 533 fletch_double y_value = Double::cast(y)->value(); | 533 dartino_double y_value = Double::cast(y)->value(); |
| 534 return process->NewDouble(x->value() - y_value); | 534 return process->NewDouble(x->value() - y_value); |
| 535 } | 535 } |
| 536 END_NATIVE() | 536 END_NATIVE() |
| 537 | 537 |
| 538 BEGIN_NATIVE(DoubleMul) { | 538 BEGIN_NATIVE(DoubleMul) { |
| 539 Double* x = Double::cast(arguments[0]); | 539 Double* x = Double::cast(arguments[0]); |
| 540 Object* y = arguments[1]; | 540 Object* y = arguments[1]; |
| 541 if (!y->IsDouble()) return Failure::wrong_argument_type(); | 541 if (!y->IsDouble()) return Failure::wrong_argument_type(); |
| 542 fletch_double y_value = Double::cast(y)->value(); | 542 dartino_double y_value = Double::cast(y)->value(); |
| 543 return process->NewDouble(x->value() * y_value); | 543 return process->NewDouble(x->value() * y_value); |
| 544 } | 544 } |
| 545 END_NATIVE() | 545 END_NATIVE() |
| 546 | 546 |
| 547 BEGIN_NATIVE(DoubleMod) { | 547 BEGIN_NATIVE(DoubleMod) { |
| 548 Double* x = Double::cast(arguments[0]); | 548 Double* x = Double::cast(arguments[0]); |
| 549 Object* y = arguments[1]; | 549 Object* y = arguments[1]; |
| 550 if (!y->IsDouble()) return Failure::wrong_argument_type(); | 550 if (!y->IsDouble()) return Failure::wrong_argument_type(); |
| 551 fletch_double y_value = Double::cast(y)->value(); | 551 dartino_double y_value = Double::cast(y)->value(); |
| 552 return process->NewDouble(fmod(x->value(), y_value)); | 552 return process->NewDouble(fmod(x->value(), y_value)); |
| 553 } | 553 } |
| 554 END_NATIVE() | 554 END_NATIVE() |
| 555 | 555 |
| 556 BEGIN_NATIVE(DoubleDiv) { | 556 BEGIN_NATIVE(DoubleDiv) { |
| 557 Double* x = Double::cast(arguments[0]); | 557 Double* x = Double::cast(arguments[0]); |
| 558 Object* y = arguments[1]; | 558 Object* y = arguments[1]; |
| 559 if (!y->IsDouble()) return Failure::wrong_argument_type(); | 559 if (!y->IsDouble()) return Failure::wrong_argument_type(); |
| 560 fletch_double y_value = Double::cast(y)->value(); | 560 dartino_double y_value = Double::cast(y)->value(); |
| 561 return process->NewDouble(x->value() / y_value); | 561 return process->NewDouble(x->value() / y_value); |
| 562 } | 562 } |
| 563 END_NATIVE() | 563 END_NATIVE() |
| 564 | 564 |
| 565 BEGIN_NATIVE(DoubleTruncDiv) { | 565 BEGIN_NATIVE(DoubleTruncDiv) { |
| 566 Double* x = Double::cast(arguments[0]); | 566 Double* x = Double::cast(arguments[0]); |
| 567 Object* y = arguments[1]; | 567 Object* y = arguments[1]; |
| 568 if (!y->IsDouble()) return Failure::wrong_argument_type(); | 568 if (!y->IsDouble()) return Failure::wrong_argument_type(); |
| 569 fletch_double y_value = Double::cast(y)->value(); | 569 dartino_double y_value = Double::cast(y)->value(); |
| 570 if (y_value == 0) return Failure::index_out_of_bounds(); | 570 if (y_value == 0) return Failure::index_out_of_bounds(); |
| 571 return process->NewInteger(static_cast<int64>(x->value() / y_value)); | 571 return process->NewInteger(static_cast<int64>(x->value() / y_value)); |
| 572 } | 572 } |
| 573 END_NATIVE() | 573 END_NATIVE() |
| 574 | 574 |
| 575 BEGIN_NATIVE(DoubleEqual) { | 575 BEGIN_NATIVE(DoubleEqual) { |
| 576 Double* x = Double::cast(arguments[0]); | 576 Double* x = Double::cast(arguments[0]); |
| 577 Object* y = arguments[1]; | 577 Object* y = arguments[1]; |
| 578 if (!y->IsDouble()) return Failure::wrong_argument_type(); | 578 if (!y->IsDouble()) return Failure::wrong_argument_type(); |
| 579 fletch_double y_value = Double::cast(y)->value(); | 579 dartino_double y_value = Double::cast(y)->value(); |
| 580 return ToBool(process, x->value() == y_value); | 580 return ToBool(process, x->value() == y_value); |
| 581 } | 581 } |
| 582 END_NATIVE() | 582 END_NATIVE() |
| 583 | 583 |
| 584 BEGIN_NATIVE(DoubleLess) { | 584 BEGIN_NATIVE(DoubleLess) { |
| 585 Double* x = Double::cast(arguments[0]); | 585 Double* x = Double::cast(arguments[0]); |
| 586 Object* y = arguments[1]; | 586 Object* y = arguments[1]; |
| 587 if (!y->IsDouble()) return Failure::wrong_argument_type(); | 587 if (!y->IsDouble()) return Failure::wrong_argument_type(); |
| 588 fletch_double y_value = Double::cast(y)->value(); | 588 dartino_double y_value = Double::cast(y)->value(); |
| 589 return ToBool(process, x->value() < y_value); | 589 return ToBool(process, x->value() < y_value); |
| 590 } | 590 } |
| 591 END_NATIVE() | 591 END_NATIVE() |
| 592 | 592 |
| 593 BEGIN_NATIVE(DoubleLessEqual) { | 593 BEGIN_NATIVE(DoubleLessEqual) { |
| 594 Double* x = Double::cast(arguments[0]); | 594 Double* x = Double::cast(arguments[0]); |
| 595 Object* y = arguments[1]; | 595 Object* y = arguments[1]; |
| 596 if (!y->IsDouble()) return Failure::wrong_argument_type(); | 596 if (!y->IsDouble()) return Failure::wrong_argument_type(); |
| 597 fletch_double y_value = Double::cast(y)->value(); | 597 dartino_double y_value = Double::cast(y)->value(); |
| 598 return ToBool(process, x->value() <= y_value); | 598 return ToBool(process, x->value() <= y_value); |
| 599 } | 599 } |
| 600 END_NATIVE() | 600 END_NATIVE() |
| 601 | 601 |
| 602 BEGIN_NATIVE(DoubleGreater) { | 602 BEGIN_NATIVE(DoubleGreater) { |
| 603 Double* x = Double::cast(arguments[0]); | 603 Double* x = Double::cast(arguments[0]); |
| 604 Object* y = arguments[1]; | 604 Object* y = arguments[1]; |
| 605 if (!y->IsDouble()) return Failure::wrong_argument_type(); | 605 if (!y->IsDouble()) return Failure::wrong_argument_type(); |
| 606 fletch_double y_value = Double::cast(y)->value(); | 606 dartino_double y_value = Double::cast(y)->value(); |
| 607 return ToBool(process, x->value() > y_value); | 607 return ToBool(process, x->value() > y_value); |
| 608 } | 608 } |
| 609 END_NATIVE() | 609 END_NATIVE() |
| 610 | 610 |
| 611 BEGIN_NATIVE(DoubleGreaterEqual) { | 611 BEGIN_NATIVE(DoubleGreaterEqual) { |
| 612 Double* x = Double::cast(arguments[0]); | 612 Double* x = Double::cast(arguments[0]); |
| 613 Object* y = arguments[1]; | 613 Object* y = arguments[1]; |
| 614 if (!y->IsDouble()) return Failure::wrong_argument_type(); | 614 if (!y->IsDouble()) return Failure::wrong_argument_type(); |
| 615 fletch_double y_value = Double::cast(y)->value(); | 615 dartino_double y_value = Double::cast(y)->value(); |
| 616 return ToBool(process, x->value() >= y_value); | 616 return ToBool(process, x->value() >= y_value); |
| 617 } | 617 } |
| 618 END_NATIVE() | 618 END_NATIVE() |
| 619 | 619 |
| 620 BEGIN_NATIVE(DoubleIsNaN) { | 620 BEGIN_NATIVE(DoubleIsNaN) { |
| 621 fletch_double d = Double::cast(arguments[0])->value(); | 621 dartino_double d = Double::cast(arguments[0])->value(); |
| 622 return ToBool(process, isnan(static_cast<double>(d))); | 622 return ToBool(process, isnan(static_cast<double>(d))); |
| 623 } | 623 } |
| 624 END_NATIVE() | 624 END_NATIVE() |
| 625 | 625 |
| 626 BEGIN_NATIVE(DoubleIsNegative) { | 626 BEGIN_NATIVE(DoubleIsNegative) { |
| 627 // TODO(ajohnsen): Okay to always use double version? | 627 // TODO(ajohnsen): Okay to always use double version? |
| 628 double d = static_cast<double>(Double::cast(arguments[0])->value()); | 628 double d = static_cast<double>(Double::cast(arguments[0])->value()); |
| 629 return ToBool(process, (signbit(d) != 0) && !isnan(d)); | 629 return ToBool(process, (signbit(d) != 0) && !isnan(d)); |
| 630 } | 630 } |
| 631 END_NATIVE() | 631 END_NATIVE() |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 843 // The string is trimmed, so we must accept the full string. | 843 // The string is trimmed, so we must accept the full string. |
| 844 if (consumed != length) return Failure::index_out_of_bounds(); | 844 if (consumed != length) return Failure::index_out_of_bounds(); |
| 845 return process->NewDouble(result); | 845 return process->NewDouble(result); |
| 846 } | 846 } |
| 847 END_NATIVE() | 847 END_NATIVE() |
| 848 | 848 |
| 849 #define DOUBLE_MATH_NATIVE(name, method) \ | 849 #define DOUBLE_MATH_NATIVE(name, method) \ |
| 850 BEGIN_NATIVE(name) { \ | 850 BEGIN_NATIVE(name) { \ |
| 851 Object* x = arguments[0]; \ | 851 Object* x = arguments[0]; \ |
| 852 if (!x->IsDouble()) return Failure::wrong_argument_type(); \ | 852 if (!x->IsDouble()) return Failure::wrong_argument_type(); \ |
| 853 fletch_double d = Double::cast(x)->value(); \ | 853 dartino_double d = Double::cast(x)->value(); \ |
| 854 return process->NewDouble(method(d)); \ | 854 return process->NewDouble(method(d)); \ |
| 855 } \ | 855 } \ |
| 856 END_NATIVE() | 856 END_NATIVE() |
| 857 | 857 |
| 858 DOUBLE_MATH_NATIVE(DoubleSin, sin) | 858 DOUBLE_MATH_NATIVE(DoubleSin, sin) |
| 859 DOUBLE_MATH_NATIVE(DoubleCos, cos) | 859 DOUBLE_MATH_NATIVE(DoubleCos, cos) |
| 860 DOUBLE_MATH_NATIVE(DoubleTan, tan) | 860 DOUBLE_MATH_NATIVE(DoubleTan, tan) |
| 861 DOUBLE_MATH_NATIVE(DoubleAcos, acos) | 861 DOUBLE_MATH_NATIVE(DoubleAcos, acos) |
| 862 DOUBLE_MATH_NATIVE(DoubleAsin, asin) | 862 DOUBLE_MATH_NATIVE(DoubleAsin, asin) |
| 863 DOUBLE_MATH_NATIVE(DoubleAtan, atan) | 863 DOUBLE_MATH_NATIVE(DoubleAtan, atan) |
| 864 DOUBLE_MATH_NATIVE(DoubleSqrt, sqrt) | 864 DOUBLE_MATH_NATIVE(DoubleSqrt, sqrt) |
| 865 DOUBLE_MATH_NATIVE(DoubleExp, exp) | 865 DOUBLE_MATH_NATIVE(DoubleExp, exp) |
| 866 DOUBLE_MATH_NATIVE(DoubleLog, log) | 866 DOUBLE_MATH_NATIVE(DoubleLog, log) |
| 867 | 867 |
| 868 BEGIN_NATIVE(DoubleAtan2) { | 868 BEGIN_NATIVE(DoubleAtan2) { |
| 869 Object* x = arguments[0]; | 869 Object* x = arguments[0]; |
| 870 if (!x->IsDouble()) return Failure::wrong_argument_type(); | 870 if (!x->IsDouble()) return Failure::wrong_argument_type(); |
| 871 Object* y = arguments[1]; | 871 Object* y = arguments[1]; |
| 872 if (!y->IsDouble()) return Failure::wrong_argument_type(); | 872 if (!y->IsDouble()) return Failure::wrong_argument_type(); |
| 873 fletch_double x_value = Double::cast(x)->value(); | 873 dartino_double x_value = Double::cast(x)->value(); |
| 874 fletch_double y_value = Double::cast(y)->value(); | 874 dartino_double y_value = Double::cast(y)->value(); |
| 875 return process->NewDouble(atan2(x_value, y_value)); | 875 return process->NewDouble(atan2(x_value, y_value)); |
| 876 } | 876 } |
| 877 END_NATIVE() | 877 END_NATIVE() |
| 878 | 878 |
| 879 BEGIN_NATIVE(DoublePow) { | 879 BEGIN_NATIVE(DoublePow) { |
| 880 Object* x = arguments[0]; | 880 Object* x = arguments[0]; |
| 881 if (!x->IsDouble()) return Failure::wrong_argument_type(); | 881 if (!x->IsDouble()) return Failure::wrong_argument_type(); |
| 882 Object* y = arguments[1]; | 882 Object* y = arguments[1]; |
| 883 if (!y->IsDouble()) return Failure::wrong_argument_type(); | 883 if (!y->IsDouble()) return Failure::wrong_argument_type(); |
| 884 fletch_double x_value = Double::cast(x)->value(); | 884 dartino_double x_value = Double::cast(x)->value(); |
| 885 fletch_double y_value = Double::cast(y)->value(); | 885 dartino_double y_value = Double::cast(y)->value(); |
| 886 return process->NewDouble(pow(x_value, y_value)); | 886 return process->NewDouble(pow(x_value, y_value)); |
| 887 } | 887 } |
| 888 END_NATIVE() | 888 END_NATIVE() |
| 889 | 889 |
| 890 BEGIN_NATIVE(ListNew) { | 890 BEGIN_NATIVE(ListNew) { |
| 891 Object* x = arguments[0]; | 891 Object* x = arguments[0]; |
| 892 if (!x->IsSmi()) return Failure::wrong_argument_type(); | 892 if (!x->IsSmi()) return Failure::wrong_argument_type(); |
| 893 int length = Smi::cast(arguments[0])->value(); | 893 int length = Smi::cast(arguments[0])->value(); |
| 894 if (length < 0) return Failure::index_out_of_bounds(); | 894 if (length < 0) return Failure::index_out_of_bounds(); |
| 895 return process->NewArray(length); | 895 return process->NewArray(length); |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1125 | 1125 |
| 1126 BEGIN_NATIVE(IdentityHashCode) { | 1126 BEGIN_NATIVE(IdentityHashCode) { |
| 1127 Object* object = arguments[0]; | 1127 Object* object = arguments[0]; |
| 1128 if (object->IsOneByteString()) { | 1128 if (object->IsOneByteString()) { |
| 1129 return Smi::FromWord(OneByteString::cast(object)->Hash()); | 1129 return Smi::FromWord(OneByteString::cast(object)->Hash()); |
| 1130 } else if (object->IsTwoByteString()) { | 1130 } else if (object->IsTwoByteString()) { |
| 1131 return Smi::FromWord(TwoByteString::cast(object)->Hash()); | 1131 return Smi::FromWord(TwoByteString::cast(object)->Hash()); |
| 1132 } else if (object->IsSmi() || object->IsLargeInteger()) { | 1132 } else if (object->IsSmi() || object->IsLargeInteger()) { |
| 1133 return object; | 1133 return object; |
| 1134 } else if (object->IsDouble()) { | 1134 } else if (object->IsDouble()) { |
| 1135 fletch_double value = Double::cast(object)->value(); | 1135 dartino_double value = Double::cast(object)->value(); |
| 1136 return process->ToInteger(static_cast<int64>(value)); | 1136 return process->ToInteger(static_cast<int64>(value)); |
| 1137 } else { | 1137 } else { |
| 1138 return Instance::cast(object)->LazyIdentityHashCode(process->random()); | 1138 return Instance::cast(object)->LazyIdentityHashCode(process->random()); |
| 1139 } | 1139 } |
| 1140 } | 1140 } |
| 1141 END_NATIVE() | 1141 END_NATIVE() |
| 1142 | 1142 |
| 1143 char* AsForeignString(Object* object) { | 1143 char* AsForeignString(Object* object) { |
| 1144 if (object->IsOneByteString()) { | 1144 if (object->IsOneByteString()) { |
| 1145 return OneByteString::cast(object)->ToCString(); | 1145 return OneByteString::cast(object)->ToCString(); |
| (...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1513 | 1513 |
| 1514 BEGIN_NATIVE(EventHandlerSleep) { | 1514 BEGIN_NATIVE(EventHandlerSleep) { |
| 1515 int64 timeout = | 1515 int64 timeout = |
| 1516 AsForeignInt64(arguments[0]) + Platform::GetMicroseconds() / 1000; | 1516 AsForeignInt64(arguments[0]) + Platform::GetMicroseconds() / 1000; |
| 1517 Port* port = Port::FromDartObject(arguments[1]); | 1517 Port* port = Port::FromDartObject(arguments[1]); |
| 1518 EventHandler::GlobalInstance()->ScheduleTimeout(timeout, port); | 1518 EventHandler::GlobalInstance()->ScheduleTimeout(timeout, port); |
| 1519 return process->program()->null_object(); | 1519 return process->program()->null_object(); |
| 1520 } | 1520 } |
| 1521 END_NATIVE() | 1521 END_NATIVE() |
| 1522 | 1522 |
| 1523 } // namespace fletch | 1523 } // namespace dartino |
| OLD | NEW |