Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/NativeScript/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ set(HEADER_FILES
ObjC/Inheritance/ObjCClassBuilder.h
ObjC/Inheritance/ObjCExtend.h
ObjC/Inheritance/ObjCTypeScriptExtend.h
ObjC/NSErrorWrapperConstructor.h
ObjC/NativeErrorWrapperConstructor.h
ObjC/ObjCMethodCall.h
ObjC/ObjCMethodCallback.h
ObjC/ObjCPrimitiveTypes.h
Expand Down Expand Up @@ -89,12 +89,12 @@ set(HEADER_FILES
)

set(SOURCE_FILES
Calling/FFICall.cpp
Calling/FFICall.mm
Calling/FFICallPrototype.cpp
Calling/FFIFunctionCall.mm
Calling/FFIFunctionCallback.cpp
GlobalObject.mm
GlobalObject.moduleLoader.mm
GlobalObject.moduleLoader.mm
inspector/CachedResource.mm
inspector/DomainBackendDispatcher.cpp
inspector/DomainInspectorAgent.cpp
Expand Down Expand Up @@ -143,7 +143,7 @@ set(SOURCE_FILES
ObjC/Inheritance/ObjCClassBuilder.mm
ObjC/Inheritance/ObjCExtend.mm
ObjC/Inheritance/ObjCTypeScriptExtend.mm
ObjC/NSErrorWrapperConstructor.mm
ObjC/NativeErrorWrapperConstructor.mm
ObjC/ObjCMethodCall.mm
ObjC/ObjCMethodCallback.mm
ObjC/ObjCPrimitiveTypes.mm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <JavaScriptCore/JSPromiseDeferred.h>
#include <JavaScriptCore/StrongInlines.h>
#include <dispatch/dispatch.h>
#include "JSErrors.h"
#include "Interop.h"

namespace NativeScript {
using namespace JSC;
Expand Down Expand Up @@ -93,9 +95,11 @@ EncodedJSValue JSC_HOST_CALL FFICall::call(ExecState* execState) {
return JSValue::encode(execState->exception());
}

{
@try {
JSLock::DropAllLocks locksDropper(execState);
ffi_call(callee->_cif, FFI_FN(invocation.function), invocation._buffer + callee->_returnOffset, reinterpret_cast<void**>(invocation._buffer + callee->_argsArrayOffset));
} @catch (NSException* localException) {
reportFatalErrorBeforeShutdown(execState, Exception::create(execState->vm(), interop(execState)->wrapException(execState, localException)));
}

JSValue result = callee->_returnType.read(execState, invocation._buffer + callee->_returnOffset, callee->_returnTypeCell.get());
Expand Down
6 changes: 4 additions & 2 deletions src/NativeScript/Interop.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace NativeScript {
class PointerInstance;
class ReferenceInstance;
class NSErrorWrapperConstructor;
class NativeErrorWrapperConstructor;

void* tryHandleofValue(const JSC::JSValue&, bool*);

Expand Down Expand Up @@ -61,6 +61,8 @@ class Interop : public JSC::JSObject {

#ifdef __OBJC__
JSC::ErrorInstance* wrapError(JSC::ExecState*, NSError*) const;

JSC::ErrorInstance* wrapException(JSC::ExecState*, NSException*) const;
#endif

private:
Expand All @@ -80,7 +82,7 @@ class Interop : public JSC::JSObject {

JSC::WriteBarrier<JSC::Structure> _functionReferenceInstanceStructure;

JSC::WriteBarrier<NSErrorWrapperConstructor> _nsErrorWrapperConstructor;
JSC::WriteBarrier<NativeErrorWrapperConstructor> _nsErrorWrapperConstructor;

JSC::WeakGCMap<const void*, PointerInstance> _pointerToInstance;

Expand Down
12 changes: 9 additions & 3 deletions src/NativeScript/Interop.mm
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#include "FunctionReferenceTypeInstance.h"
#include "FunctionReferenceTypeConstructor.h"
#include "ObjCTypes.h"
#include "NSErrorWrapperConstructor.h"
#include "NativeErrorWrapperConstructor.h"

namespace NativeScript {
using namespace JSC;
Expand Down Expand Up @@ -244,8 +244,8 @@ static EncodedJSValue JSC_HOST_CALL interopFuncBufferFromData(ExecState* execSta
this->putDirect(vm, Identifier::fromString(&vm, functionReferenceConstructor->name(globalObject->globalExec())), functionReferenceConstructor, ReadOnly | DontDelete);
functionReferencePrototype->putDirect(vm, vm.propertyNames->constructor, functionReferenceConstructor, DontEnum);

this->_nsErrorWrapperConstructor.set(vm, this, NSErrorWrapperConstructor::create(vm, NSErrorWrapperConstructor::createStructure(vm, globalObject, globalObject->functionPrototype())));
this->putDirect(vm, Identifier::fromString(&vm, "NSErrorWrapper"), this->_nsErrorWrapperConstructor.get());
this->_nsErrorWrapperConstructor.set(vm, this, NativeErrorWrapperConstructor::create(vm, NativeErrorWrapperConstructor::createStructure(vm, globalObject, globalObject->functionPrototype())));
this->putDirect(vm, Identifier::fromString(&vm, "NativeErrorWrapper"), this->_nsErrorWrapperConstructor.get());

this->putDirectNativeFunction(vm, globalObject, Identifier::fromString(&vm, WTF::ASCIILiteral("alloc")), 0, &interopFuncAlloc, NoIntrinsic, ReadOnly | DontDelete);
this->putDirectNativeFunction(vm, globalObject, Identifier::fromString(&vm, WTF::ASCIILiteral("free")), 0, &interopFuncFree, NoIntrinsic, ReadOnly | DontDelete);
Expand Down Expand Up @@ -335,15 +335,21 @@ static EncodedJSValue JSC_HOST_CALL interopFuncBufferFromData(ExecState* execSta
visitor.append(&interop->_nsErrorWrapperConstructor);
}

#ifdef __OBJC__
ErrorInstance* Interop::wrapError(ExecState* execState, NSError* error) const {
return this->_nsErrorWrapperConstructor->createError(execState, error);
}

ErrorInstance* Interop::wrapException(ExecState* execState, NSException* exception) const {
return this->_nsErrorWrapperConstructor->createError(execState, exception);
}

JSArrayBuffer* Interop::bufferFromData(ExecState* execState, NSData* data) const {
JSArrayBuffer* arrayBuffer = JSArrayBuffer::create(execState->vm(), execState->lexicalGlobalObject()->arrayBufferStructure(), ArrayBuffer::createAdopted([data bytes], [data length], false));

// make the ArrayBuffer hold on to the NSData instance so as to keep its bytes alive
arrayBuffer->putDirect(execState->vm(), execState->propertyNames().homeObjectPrivateName, NativeScript::toValue(execState, data));
return arrayBuffer;
}
#endif
}
67 changes: 0 additions & 67 deletions src/NativeScript/ObjC/NSErrorWrapperConstructor.mm

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// NSErrorWrapperConstructor.h
// NativeErrorWrapperConstructor.h
// NativeScript
//
// Created by Yavor Georgiev on 30.12.15 г..
Expand All @@ -11,12 +11,12 @@
@class NSError;

namespace NativeScript {
class NSErrorWrapperConstructor : public JSC::InternalFunction {
class NativeErrorWrapperConstructor : public JSC::InternalFunction {
public:
typedef JSC::InternalFunction Base;

static NSErrorWrapperConstructor* create(JSC::VM& vm, JSC::Structure* structure) {
NSErrorWrapperConstructor* cell = new (NotNull, JSC::allocateCell<NSErrorWrapperConstructor>(vm.heap)) NSErrorWrapperConstructor(vm, structure);
static NativeErrorWrapperConstructor* create(JSC::VM& vm, JSC::Structure* structure) {
NativeErrorWrapperConstructor* cell = new (NotNull, JSC::allocateCell<NativeErrorWrapperConstructor>(vm.heap)) NativeErrorWrapperConstructor(vm, structure);
cell->finishCreation(vm, structure->globalObject());
return cell;
}
Expand All @@ -33,8 +33,10 @@ class NSErrorWrapperConstructor : public JSC::InternalFunction {

JSC::ErrorInstance* createError(JSC::ExecState*, NSError*) const;

JSC::ErrorInstance* createError(JSC::ExecState*, NSException*) const;

private:
NSErrorWrapperConstructor(JSC::VM& vm, JSC::Structure* structure)
NativeErrorWrapperConstructor(JSC::VM& vm, JSC::Structure* structure)
: Base(vm, structure) {
}

Expand Down
72 changes: 72 additions & 0 deletions src/NativeScript/ObjC/NativeErrorWrapperConstructor.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// NativeErrorWrapperConstructor.mm
// NativeScript
//
// Created by Yavor Georgiev on 30.12.15 г..
// Copyright (c) 2015 Telerik. All rights reserved.
//

#include "NativeErrorWrapperConstructor.h"
#include "ObjCTypes.h"
#include <JavaScriptCore/ErrorPrototype.h>

namespace NativeScript {
using namespace JSC;

const ClassInfo NativeErrorWrapperConstructor::s_info = { "NativeException", &Base::s_info, 0, CREATE_METHOD_TABLE(NativeErrorWrapperConstructor) };

void NativeErrorWrapperConstructor::destroy(JSCell* cell) {
jsCast<NativeErrorWrapperConstructor*>(cell)->~NativeErrorWrapperConstructor();
}

void NativeErrorWrapperConstructor::finishCreation(VM& vm, JSGlobalObject* globalObject) {
Base::finishCreation(vm, WTF::ASCIILiteral("NativeException"));

ErrorPrototype* prototype = ErrorPrototype::create(vm, globalObject, ErrorPrototype::createStructure(vm, globalObject, globalObject->errorPrototype()));
prototype->putDirect(vm, vm.propertyNames->constructor, this);
this->putDirect(vm, vm.propertyNames->prototype, prototype);
prototype->putDirect(vm, vm.propertyNames->name, jsString(&vm, WTF::ASCIILiteral("NativeException")));

this->_errorStructure.set(vm, this, ErrorInstance::createStructure(vm, globalObject, prototype));
}

void NativeErrorWrapperConstructor::visitChildren(JSCell* cell, SlotVisitor& slotVisitor) {
Base::visitChildren(cell, slotVisitor);

NativeErrorWrapperConstructor* self = jsCast<NativeErrorWrapperConstructor*>(cell);
slotVisitor.append(&self->_errorStructure);
}

ErrorInstance* NativeErrorWrapperConstructor::createError(ExecState* execState, NSError* error) const {
ErrorInstance* wrappedError = ErrorInstance::create(execState, this->errorStructure(), jsString(execState, error.localizedDescription));
wrappedError->putDirect(execState->vm(), Identifier::fromString(execState, "nativeException"), NativeScript::toValue(execState, error));
return wrappedError;
}

ErrorInstance* NativeErrorWrapperConstructor::createError(ExecState* execState, NSException* exception) const {
ErrorInstance* wrappedError = ErrorInstance::create(execState, this->errorStructure(), jsString(execState, exception.reason));
wrappedError->putDirect(execState->vm(), Identifier::fromString(execState, "nativeException"), NativeScript::toValue(execState, exception));
return wrappedError;
}

static EncodedJSValue JSC_HOST_CALL constructErrorWrapper(ExecState* execState) {
NativeErrorWrapperConstructor* self = jsCast<NativeErrorWrapperConstructor*>(execState->callee());
NSError* error = NativeScript::toObject(execState, execState->argument(0));

if (!error || ![error isKindOfClass:[NSError class]]) {
return throwVMTypeError(execState);
}

return JSValue::encode(self->createError(execState, error));
}

ConstructType NativeErrorWrapperConstructor::getConstructData(JSCell*, ConstructData& constructData) {
constructData.native.function = &constructErrorWrapper;
return ConstructTypeHost;
}

CallType NativeErrorWrapperConstructor::getCallData(JSCell*, CallData& callData) {
callData.native.function = &constructErrorWrapper;
return CallTypeHost;
}
}
1 change: 1 addition & 0 deletions tests/TestFixtures/exported-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ functionReturningFunctionPtrAsVoidPtr
functionReturnsCFRetained
functionReturnsNSRetained
functionReturnsUnmanaged
functionThrowsException
functionWhichReturnsSimpleFunctionPointer
functionWith_BoolPtr
functionWith_VoidPtr
Expand Down
4 changes: 2 additions & 2 deletions tests/TestRunner/app/ApiTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ describe(module.id, function () {
TNSApi.new().methodError(1);
} catch (e) {
isThrown = true;
expect(e instanceof interop.NSErrorWrapper).toBe(true);
expect(e instanceof interop.NativeErrorWrapper).toBe(true);
expect(e.stack).toEqual(jasmine.any(String));
} finally {
expect(isThrown).toBe(true);
Expand Down Expand Up @@ -435,7 +435,7 @@ describe(module.id, function () {
var api = TNSApi.new();
api.methodError.async(api, [1])
.catch(error => {
expect(error).toEqual(jasmine.any(interop.NSErrorWrapper));
expect(error).toEqual(jasmine.any(interop.NativeErrorWrapper));
done();
});
});
Expand Down