Files
sdk/runtime/vm/native_api_impl.cc
T
Siva Annamalai 9e19d236ca - Add an OSThread structure which is the generic TLS structure for all C++
fields in a thread (i.e fields that are not Dart VM related)
- Split the Thread structure to be a pure Dart per thread structure and add
  a pointer to os_thread which points to the OSThread structure
- Change Schedule/UnSchedule to set the Dart Thread structure as the TLS of
  the thread when it is inside the Dart world and reset the TLS back to the
  OSThread strcuture when is exits the Dart World.
- Moved the stack_base and few stack size related functions to OSThread from Isolate

R=johnmccutchan@google.com, zra@google.com

Review URL: https://codereview.chromium.org/1439483003 .
2015-11-19 13:45:10 -08:00

114 lines
3.1 KiB
C++

// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "include/dart_native_api.h"
#include "platform/assert.h"
#include "vm/dart_api_impl.h"
#include "vm/dart_api_message.h"
#include "vm/dart_api_state.h"
#include "vm/message.h"
#include "vm/native_message_handler.h"
#include "vm/port.h"
namespace dart {
// --- Message sending/receiving from native code ---
static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) {
void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size);
return reinterpret_cast<uint8_t*>(new_ptr);
}
class IsolateSaver {
public:
explicit IsolateSaver(Isolate* current_isolate)
: saved_isolate_(current_isolate) {
if (current_isolate != NULL) {
ASSERT(current_isolate == Isolate::Current());
Thread::ExitIsolate();
}
}
~IsolateSaver() {
if (saved_isolate_ != NULL) {
Thread::EnterIsolate(saved_isolate_);
}
}
private:
Isolate* saved_isolate_;
DISALLOW_COPY_AND_ASSIGN(IsolateSaver);
};
DART_EXPORT bool Dart_PostCObject(Dart_Port port_id, Dart_CObject* message) {
uint8_t* buffer = NULL;
ApiMessageWriter writer(&buffer, allocator);
bool success = writer.WriteCMessage(message);
if (!success) return success;
// Post the message at the given port.
return PortMap::PostMessage(new Message(
port_id, buffer, writer.BytesWritten(), Message::kNormalPriority));
}
DART_EXPORT Dart_Port Dart_NewNativePort(const char* name,
Dart_NativeMessageHandler handler,
bool handle_concurrently) {
if (name == NULL) {
name = "<UnnamedNativePort>";
}
if (handler == NULL) {
OS::PrintErr("%s expects argument 'handler' to be non-null.\n",
CURRENT_FUNC);
return ILLEGAL_PORT;
}
// Start the native port without a current isolate.
IsolateSaver saver(Isolate::Current());
NativeMessageHandler* nmh = new NativeMessageHandler(name, handler);
Dart_Port port_id = PortMap::CreatePort(nmh);
nmh->Run(Dart::thread_pool(), NULL, NULL, 0);
return port_id;
}
DART_EXPORT bool Dart_CloseNativePort(Dart_Port native_port_id) {
// Close the native port without a current isolate.
IsolateSaver saver(Isolate::Current());
// TODO(turnidge): Check that the port is native before trying to close.
return PortMap::ClosePort(native_port_id);
}
// --- Verification tools ---
static void CompileAll(Thread* thread, Dart_Handle* result) {
ASSERT(thread != NULL);
const Error& error = Error::Handle(thread->zone(), Library::CompileAll());
if (error.IsNull()) {
*result = Api::Success();
} else {
*result = Api::NewHandle(thread->isolate(), error.raw());
}
}
DART_EXPORT Dart_Handle Dart_CompileAll() {
DARTSCOPE(Thread::Current());
Dart_Handle result = Api::CheckAndFinalizePendingClasses(I);
if (::Dart_IsError(result)) {
return result;
}
CHECK_CALLBACK_STATE(T);
CompileAll(T, &result);
return result;
}
} // namespace dart