| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart 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 file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/bootstrap_natives.h" | 5 #include "vm/bootstrap_natives.h" |
| 6 | 6 |
| 7 #include "vm/compiler.h" | 7 #include "vm/compiler.h" |
| 8 #include "vm/dart_entry.h" | 8 #include "vm/dart_entry.h" |
| 9 #include "vm/exceptions.h" | 9 #include "vm/exceptions.h" |
| 10 #include "vm/native_entry.h" | 10 #include "vm/native_entry.h" |
| 11 #include "vm/object.h" | 11 #include "vm/object.h" |
| 12 #include "vm/symbols.h" | 12 #include "vm/symbols.h" |
| 13 | 13 |
| 14 namespace dart { | 14 namespace dart { |
| 15 | 15 |
| 16 DEFINE_NATIVE_ENTRY(Function_apply, 2) { | 16 DEFINE_NATIVE_ENTRY(Function_apply, 2) { |
| 17 const Array& fun_arguments = Array::CheckedHandle(arguments->NativeArgAt(0)); | 17 const Array& fun_arguments = Array::CheckedHandle(arguments->NativeArgAt(0)); |
| 18 const Array& fun_arg_names = Array::CheckedHandle(arguments->NativeArgAt(1)); | 18 const Array& fun_arg_names = Array::CheckedHandle(arguments->NativeArgAt(1)); |
| 19 const Array& fun_args_desc = | 19 const Array& fun_args_desc = Array::Handle( |
| 20 Array::Handle(ArgumentsDescriptor::New(fun_arguments.Length(), | 20 ArgumentsDescriptor::New(fun_arguments.Length(), fun_arg_names)); |
| 21 fun_arg_names)); | |
| 22 const Object& result = | 21 const Object& result = |
| 23 Object::Handle(DartEntry::InvokeClosure(fun_arguments, fun_args_desc)); | 22 Object::Handle(DartEntry::InvokeClosure(fun_arguments, fun_args_desc)); |
| 24 if (result.IsError()) { | 23 if (result.IsError()) { |
| 25 Exceptions::PropagateError(Error::Cast(result)); | 24 Exceptions::PropagateError(Error::Cast(result)); |
| 26 } | 25 } |
| 27 return result.raw(); | 26 return result.raw(); |
| 28 } | 27 } |
| 29 | 28 |
| 30 | 29 |
| 31 DEFINE_NATIVE_ENTRY(Closure_equals, 2) { | 30 DEFINE_NATIVE_ENTRY(Closure_equals, 2) { |
| 32 const Closure& receiver = Closure::CheckedHandle( | 31 const Closure& receiver = |
| 33 zone, arguments->NativeArgAt(0)); | 32 Closure::CheckedHandle(zone, arguments->NativeArgAt(0)); |
| 34 GET_NATIVE_ARGUMENT(Instance, other, arguments->NativeArgAt(1)); | 33 GET_NATIVE_ARGUMENT(Instance, other, arguments->NativeArgAt(1)); |
| 35 ASSERT(!other.IsNull()); | 34 ASSERT(!other.IsNull()); |
| 36 if (receiver.raw() == other.raw()) return Bool::True().raw(); | 35 if (receiver.raw() == other.raw()) return Bool::True().raw(); |
| 37 if (other.IsClosure()) { | 36 if (other.IsClosure()) { |
| 38 const Function& func_a = Function::Handle(receiver.function()); | 37 const Function& func_a = Function::Handle(receiver.function()); |
| 39 const Function& func_b = Function::Handle(Closure::Cast(other).function()); | 38 const Function& func_b = Function::Handle(Closure::Cast(other).function()); |
| 40 if (func_a.raw() == func_b.raw()) { | 39 if (func_a.raw() == func_b.raw()) { |
| 41 ASSERT(!func_a.IsImplicitStaticClosureFunction()); | 40 ASSERT(!func_a.IsImplicitStaticClosureFunction()); |
| 42 if (func_a.IsImplicitInstanceClosureFunction()) { | 41 if (func_a.IsImplicitInstanceClosureFunction()) { |
| 43 const Context& context_a = Context::Handle(receiver.context()); | 42 const Context& context_a = Context::Handle(receiver.context()); |
| 44 const Context& context_b = Context::Handle( | 43 const Context& context_b = |
| 45 Closure::Cast(other).context()); | 44 Context::Handle(Closure::Cast(other).context()); |
| 46 const Object& receiver_a = Object::Handle(context_a.At(0)); | 45 const Object& receiver_a = Object::Handle(context_a.At(0)); |
| 47 const Object& receiver_b = Object::Handle(context_b.At(0)); | 46 const Object& receiver_b = Object::Handle(context_b.At(0)); |
| 48 if (receiver_a.raw() == receiver_b.raw()) return Bool::True().raw(); | 47 if (receiver_a.raw() == receiver_b.raw()) return Bool::True().raw(); |
| 49 } | 48 } |
| 50 } else if (func_a.IsImplicitInstanceClosureFunction() && | 49 } else if (func_a.IsImplicitInstanceClosureFunction() && |
| 51 func_b.IsImplicitInstanceClosureFunction()) { | 50 func_b.IsImplicitInstanceClosureFunction()) { |
| 52 // TODO(rmacnak): Patch existing tears off during reload instead. | 51 // TODO(rmacnak): Patch existing tears off during reload instead. |
| 53 const Context& context_a = Context::Handle(receiver.context()); | 52 const Context& context_a = Context::Handle(receiver.context()); |
| 54 const Context& context_b = Context::Handle( | 53 const Context& context_b = |
| 55 Closure::Cast(other).context()); | 54 Context::Handle(Closure::Cast(other).context()); |
| 56 const Object& receiver_a = Object::Handle(context_a.At(0)); | 55 const Object& receiver_a = Object::Handle(context_a.At(0)); |
| 57 const Object& receiver_b = Object::Handle(context_b.At(0)); | 56 const Object& receiver_b = Object::Handle(context_b.At(0)); |
| 58 if ((receiver_a.raw() == receiver_b.raw()) && | 57 if ((receiver_a.raw() == receiver_b.raw()) && |
| 59 (func_a.name() == func_b.name()) && | 58 (func_a.name() == func_b.name()) && |
| 60 (func_a.Owner() == func_b.Owner())) { | 59 (func_a.Owner() == func_b.Owner())) { |
| 61 return Bool::True().raw(); | 60 return Bool::True().raw(); |
| 62 } | 61 } |
| 63 } | 62 } |
| 64 } | 63 } |
| 65 return Bool::False().raw(); | 64 return Bool::False().raw(); |
| 66 } | 65 } |
| 67 | 66 |
| 68 | 67 |
| 69 DEFINE_NATIVE_ENTRY(Closure_hashCode, 1) { | 68 DEFINE_NATIVE_ENTRY(Closure_hashCode, 1) { |
| 70 const Closure& receiver = Closure::CheckedHandle( | 69 const Closure& receiver = |
| 71 zone, arguments->NativeArgAt(0)); | 70 Closure::CheckedHandle(zone, arguments->NativeArgAt(0)); |
| 72 const Function& func = Function::Handle(receiver.function()); | 71 const Function& func = Function::Handle(receiver.function()); |
| 73 // Hash together name, class name and signature. | 72 // Hash together name, class name and signature. |
| 74 const Class& cls = Class::Handle(func.Owner()); | 73 const Class& cls = Class::Handle(func.Owner()); |
| 75 intptr_t result = String::Handle(func.name()).Hash(); | 74 intptr_t result = String::Handle(func.name()).Hash(); |
| 76 result += String::Handle(func.Signature()).Hash(); | 75 result += String::Handle(func.Signature()).Hash(); |
| 77 result += String::Handle(cls.Name()).Hash(); | 76 result += String::Handle(cls.Name()).Hash(); |
| 78 // Finalize hash value like for strings so that it fits into a smi. | 77 // Finalize hash value like for strings so that it fits into a smi. |
| 79 result += result << 3; | 78 result += result << 3; |
| 80 result ^= result >> 11; | 79 result ^= result >> 11; |
| 81 result += result << 15; | 80 result += result << 15; |
| 82 result &= ((static_cast<intptr_t>(1) << String::kHashBits) - 1); | 81 result &= ((static_cast<intptr_t>(1) << String::kHashBits) - 1); |
| 83 return Smi::New(result); | 82 return Smi::New(result); |
| 84 } | 83 } |
| 85 | 84 |
| 86 | 85 |
| 87 DEFINE_NATIVE_ENTRY(Closure_clone, 1) { | 86 DEFINE_NATIVE_ENTRY(Closure_clone, 1) { |
| 88 const Closure& receiver = Closure::CheckedHandle( | 87 const Closure& receiver = |
| 89 zone, arguments->NativeArgAt(0)); | 88 Closure::CheckedHandle(zone, arguments->NativeArgAt(0)); |
| 90 const Function& func = Function::Handle(zone, receiver.function()); | 89 const Function& func = Function::Handle(zone, receiver.function()); |
| 91 const Context& ctx = Context::Handle(zone, receiver.context()); | 90 const Context& ctx = Context::Handle(zone, receiver.context()); |
| 92 Context& cloned_ctx = | 91 Context& cloned_ctx = |
| 93 Context::Handle(zone, Context::New(ctx.num_variables())); | 92 Context::Handle(zone, Context::New(ctx.num_variables())); |
| 94 cloned_ctx.set_parent(Context::Handle(zone, ctx.parent())); | 93 cloned_ctx.set_parent(Context::Handle(zone, ctx.parent())); |
| 95 Object& inst = Object::Handle(zone); | 94 Object& inst = Object::Handle(zone); |
| 96 for (int i = 0; i < ctx.num_variables(); i++) { | 95 for (int i = 0; i < ctx.num_variables(); i++) { |
| 97 inst = ctx.At(i); | 96 inst = ctx.At(i); |
| 98 cloned_ctx.SetAt(i, inst); | 97 cloned_ctx.SetAt(i, inst); |
| 99 } | 98 } |
| 100 return Closure::New(func, cloned_ctx); | 99 return Closure::New(func, cloned_ctx); |
| 101 } | 100 } |
| 102 | 101 |
| 103 | 102 |
| 104 } // namespace dart | 103 } // namespace dart |
| OLD | NEW |