| 67 | } |
| 68 | |
| 69 | static Array::Ptr ArraySort(const std::vector<Value>& args) |
| 70 | { |
| 71 | ScriptFrame *vframe = ScriptFrame::GetCurrentFrame(); |
| 72 | Array::Ptr self = static_cast<Array::Ptr>(vframe->Self); |
| 73 | REQUIRE_NOT_NULL(self); |
| 74 | |
| 75 | Array::Ptr arr = self->ShallowClone(); |
| 76 | |
| 77 | if (args.empty()) { |
| 78 | ObjectLock olock(arr); |
| 79 | std::sort(arr->Begin(), arr->End()); |
| 80 | } else { |
| 81 | Function::Ptr function = args[0]; |
| 82 | REQUIRE_NOT_NULL(function); |
| 83 | |
| 84 | if (vframe->Sandboxed && !function->IsSideEffectFree()) |
| 85 | BOOST_THROW_EXCEPTION(ScriptError("Sort function must be side-effect free.")); |
| 86 | |
| 87 | ObjectLock olock(arr); |
| 88 | std::sort(arr->Begin(), arr->End(), [&args](const Value& a, const Value& b) -> bool { |
| 89 | Function::Ptr cmp = args[0]; |
| 90 | return cmp->Invoke({ a, b }); |
| 91 | }); |
| 92 | } |
| 93 | |
| 94 | return arr; |
| 95 | } |
| 96 | |
| 97 | static Array::Ptr ArrayShallowClone() |
| 98 | { |
nothing calls this directly
no test coverage detected