diff --git a/binding.gyp b/binding.gyp index 89f3eb6..bc0d601 100644 --- a/binding.gyp +++ b/binding.gyp @@ -21,7 +21,7 @@ # 'src/PolygonSet2.cc', # 'src/PolygonWithHoles2.cc', # 'src/Ray2.cc', - # 'src/Segment2.cc', + 'src/Segment2.cc', # 'src/Vector2.cc' ], diff --git a/spec/Segment2.nospec.js b/spec/Segment2.spec.js similarity index 100% rename from spec/Segment2.nospec.js rename to spec/Segment2.spec.js diff --git a/src/BBox2.cc b/src/BBox2.cc index 6d8b63b..e34dc52 100644 --- a/src/BBox2.cc +++ b/src/BBox2.cc @@ -1,6 +1,5 @@ #include "BBox2.h" #include "cgal_args.h" -#include "napi.h" using namespace std; @@ -52,13 +51,13 @@ bool BBox2::ParseArg(Napi::Env env, Napi::Value arg, Bbox_2& receiver) } -Napi::Value BBox2::ToPOD(Napi::Env env, bool precise) +Napi::Value BBox2::ToPOD(Napi::Env env, Bbox_2 const& box, bool precise) { Napi::Object obj = Napi::Object::New(env); - obj.Set("xmin", mWrapped.xmin()); - obj.Set("ymin", mWrapped.ymin()); - obj.Set("xmax", mWrapped.xmax()); - obj.Set("ymax", mWrapped.ymax()); + obj.Set("xmin", box.xmin()); + obj.Set("ymin", box.ymin()); + obj.Set("xmax", box.xmax()); + obj.Set("ymax", box.ymax()); return obj; } diff --git a/src/BBox2.h b/src/BBox2.h index f20abc6..644a22e 100644 --- a/src/BBox2.h +++ b/src/BBox2.h @@ -3,6 +3,7 @@ #include "CGALWrapper.h" #include "cgal_types.h" +#include "napi.h" #include @@ -15,17 +16,18 @@ class BBox2 : public CGALWrapper // The name to be used for our JS class. static char const* Name; - // Convert our wrapped CGAL object to a POD JS object. If precise is set to false, - // attempt to render in terms of doubles for coordinates, which may lose precision. + // Add our property descriptors (instance and static methods and values) to the list that will + // be used to define our JS class. Called indirectly at module load time via the module + // init function. static void AddProperties(std::vector& properties); // Attempt to parse a JS argument into the CGAL object referred to by receiver. Returns true // if parse was successful, false otherwise. static bool ParseArg(Napi::Env env, Napi::Value arg, Bbox_2& receiver); - // Convert our wrapped CGAL object to a POD JS object. If precise is set to false, - // attempt to render in terms of doubles for coordinates, which may lose precision. - Napi::Value ToPOD(Napi::Env env, bool precise); + // Convert a CGAL object of the wrapped class to a POD JS object. If precise is set to false, + // will attempt to render in terms of doubles for coordinates, and may lose precision. + static Napi::Value ToPOD(Napi::Env env, Bbox_2 const& box, bool precise); private: diff --git a/src/CGALWrapper-inl.h b/src/CGALWrapper-inl.h index 8276e5b..1ddd22d 100644 --- a/src/CGALWrapper-inl.h +++ b/src/CGALWrapper-inl.h @@ -62,11 +62,11 @@ Napi::Object CGALWrapper::New(Napi::Env env, const CGAL } -template -bool ParseArg(Napi::Env env, Napi::Value arg, NumberPrimitive& parsed) +template +bool ParseNumberArg(Napi::Env env, Napi::Value arg, NumberType& parsed) { if (arg.IsNumber()) { - parsed = NumberPrimitive(arg.As()); + parsed = arg.As(); return true; } else if (arg.IsString()) { std::istringstream str(arg.As()); @@ -130,7 +130,7 @@ Napi::Value CGALWrapper::ToPOD(Napi::CallbackInfo const precise = info[0].As(); } - return static_cast(this)->ToPOD(env, precise); + return WrapperClass::ToPOD(env, mWrapped, precise); } diff --git a/src/CGALWrapper.h b/src/CGALWrapper.h index df441d6..a7a30b9 100644 --- a/src/CGALWrapper.h +++ b/src/CGALWrapper.h @@ -17,8 +17,8 @@ class CGALWrapper : public Napi::ObjectWrap static Napi::Object New(Napi::Env env, const CGALClass &CGALInstance); - template - static bool ParseArg(Napi::Env env, Napi::Value arg, NumberPrimitive& parsed); + template + static bool ParseNumberArg(Napi::Env env, Napi::Value arg, NumberType& parsed); template static bool ParseSeqArg(Napi::Env env, Napi::Value arg, OutputIterator iterator); diff --git a/src/Point2.cc b/src/Point2.cc index d9cfb4f..a1910a0 100644 --- a/src/Point2.cc +++ b/src/Point2.cc @@ -36,8 +36,8 @@ bool Point2::ParseArg(Napi::Env env, Napi::Value arg, Point_2& receiver) if (arg.IsArray()) { Napi::Array coords = arg.As(); K::FT x, y; - if (::ParseArg(env, coords[0u], x) && - ::ParseArg(env, coords[1], y)) + if (::ParseNumberArg(env, coords[0u], x) && + ::ParseNumberArg(env, coords[1], y)) { receiver = Point_2(x, y); return true; @@ -48,27 +48,27 @@ bool Point2::ParseArg(Napi::Env env, Napi::Value arg, Point_2& receiver) } -Napi::Value Point2::ToPOD(Napi::Env env, bool precise) +Napi::Value Point2::ToPOD(Napi::Env env, Point_2 const& point, bool precise) { Napi::Array array = Napi::Array::New(env, 2); if (precise) { ostringstream x_str; #if CGAL_USE_EPECK - x_str << mWrapped.x().exact(); + x_str << point.x().exact(); #else - x_str << setprecision(20) << mWrapped.x(); + x_str << setprecision(20) << point.x(); #endif array.Set(0u, x_str.str()); ostringstream y_str; #if CGAL_USE_EPECK - y_str << mWrapped.y().exact(); + y_str << point.y().exact(); #else - y_str << setprecision(20) << mWrapped.y(); + y_str << setprecision(20) << point.y(); #endif array.Set(1, y_str.str()); } else { - array.Set(0u, CGAL::to_double(mWrapped.cartesian(0))); - array.Set(1, CGAL::to_double(mWrapped.cartesian(1))); + array.Set(0u, CGAL::to_double(point.cartesian(0))); + array.Set(1, CGAL::to_double(point.cartesian(1))); } return array; diff --git a/src/Point2.h b/src/Point2.h index 9800198..6bb80f3 100644 --- a/src/Point2.h +++ b/src/Point2.h @@ -5,6 +5,8 @@ #include "cgal_types.h" #include "napi.h" +#include + class Point2 : public CGALWrapper { public: @@ -23,9 +25,9 @@ class Point2 : public CGALWrapper // if parse was successful, false otherwise. static bool ParseArg(Napi::Env env, Napi::Value arg, Point_2& receiver); - // Convert our wrapped CGAL object to a POD JS object. If precise is set to false, - // attempt to render in terms of doubles for coordinates, which may lose precision. - Napi::Value ToPOD(Napi::Env env, bool precise); + // Convert a CGAL object of the wrapped class to a POD JS object. If precise is set to false, + // will attempt to render in terms of doubles for coordinates, and may lose precision. + static Napi::Value ToPOD(Napi::Env env, Point_2 const& point, bool precise=true); private: diff --git a/src/Segment2.cc b/src/Segment2.cc index b734886..7c7c92b 100644 --- a/src/Segment2.cc +++ b/src/Segment2.cc @@ -2,85 +2,67 @@ #include "Point2.h" #include "cgal_args.h" -using namespace v8; -using namespace node; + using namespace std; +Segment2::Segment2(Napi::CallbackInfo const& info) +: CGALWrapper(info) +{ +} + + const char *Segment2::Name = "Segment2"; -void Segment2::RegisterMethods(Isolate *isolate) +void Segment2::AddProperties(vector& properties) { - HandleScope scope(isolate); - Local constructorTemplate = sConstructorTemplate.Get(isolate); - NODE_SET_PROTOTYPE_METHOD(constructorTemplate, "isVertical", IsVertical); - NODE_SET_PROTOTYPE_METHOD(constructorTemplate, "isHorizontal", IsHorizontal); + properties.insert(properties.end(), { + InstanceMethod("isHorizontal", &Segment2::IsHorizontal), + InstanceMethod("isVertical", &Segment2::IsVertical) + }); } -bool Segment2::ParseArg(Isolate *isolate, Local arg, Segment_2 &receiver) +bool Segment2::ParseArg(Napi::Env env, Napi::Value arg, Segment_2& receiver) { - HandleScope scope(isolate); - Local context = isolate->GetCurrentContext(); - - if (sConstructorTemplate.Get(isolate)->HasInstance(arg)) { - receiver = ExtractWrapped(Local::Cast(arg)); - return true; - } + if (arg.IsObject()) { + Napi::Object obj = arg.As(); - if (arg->IsObject()) { - Local ends = Local::Cast(arg); + if (obj.InstanceOf(sConstructor.Value())) { + receiver = Unwrap(obj)->mWrapped; + return true; + } Point_2 source, target; - - if (Point2::ParseArg(isolate, ends->Get(context, SYMBOL(isolate, "source")).ToLocalChecked(), source) && - Point2::ParseArg(isolate, ends->Get(context, SYMBOL(isolate, "target")).ToLocalChecked(), target)) + if (Point2::ParseArg(env, obj["source"], source) && + Point2::ParseArg(env, obj["target"], target)) { receiver = Segment_2(source, target); return true; } - } return false; } -Local Segment2::ToPOD(Isolate *isolate, const Segment_2 &segment, bool precise) +Napi::Value Segment2::ToPOD(Napi::Env env, Segment_2 const& segment, bool precise) { - EscapableHandleScope scope(isolate); - Local context = isolate->GetCurrentContext(); - Local obj = Object::New(isolate); - obj->Set(context, SYMBOL(isolate, "source"), Point2::ToPOD(isolate, segment.source(), precise)); - obj->Set(context, SYMBOL(isolate, "target"), Point2::ToPOD(isolate, segment.target(), precise)); - return scope.Escape(obj); + Napi::Object obj = Napi::Object::New(env); + obj.Set("source", Point2::ToPOD(env, segment.source(), precise)); + obj.Set("target", Point2::ToPOD(env, segment.target(), precise)); + return obj; } -void Segment2::IsHorizontal(const FunctionCallbackInfo &info) +Napi::Value Segment2::IsHorizontal(Napi::CallbackInfo const& info) { - Isolate *isolate = info.GetIsolate(); - HandleScope scope(isolate); - try { - Segment_2 &segment = ExtractWrapped(info.This()); - info.GetReturnValue().Set(Boolean::New(isolate, segment.is_horizontal())); - } - catch (const exception &e) { - isolate->ThrowException(String::NewFromUtf8(isolate, e.what(), NewStringType::kNormal).ToLocalChecked()); - } + return Napi::Value::From(info.Env(), mWrapped.is_horizontal()); } -void Segment2::IsVertical(const FunctionCallbackInfo &info) +Napi::Value Segment2::IsVertical(Napi::CallbackInfo const& info) { - Isolate *isolate = info.GetIsolate(); - HandleScope scope(isolate); - try { - Segment_2 &segment = ExtractWrapped(info.This()); - info.GetReturnValue().Set(Boolean::New(isolate, segment.is_vertical())); - } - catch (const exception &e) { - isolate->ThrowException(String::NewFromUtf8(isolate, e.what(), NewStringType::kNormal).ToLocalChecked()); - } + return Napi::Value::From(info.Env(), mWrapped.is_vertical()); } diff --git a/src/Segment2.h b/src/Segment2.h index df531ee..cb72070 100644 --- a/src/Segment2.h +++ b/src/Segment2.h @@ -3,38 +3,39 @@ #include "CGALWrapper.h" #include "cgal_types.h" -#include "v8.h" - +#include "napi.h" class Segment2 : public CGALWrapper { public: + Segment2(Napi::CallbackInfo const& info); + // The name to be used for our JS class. static const char *Name; - // Add our function templates to the package exports, and return string to be used to name - // the class and constructor in JS. Called indirectly at module load time via the module + // Add our property descriptors (instance and static methods and values) to the list that will + // be used to define our JS class. Called indirectly at module load time via the module // init function. - static void RegisterMethods(v8::Isolate *isolate); + static void AddProperties(std::vector& properties); - // Attempt to parse a v8 argument into the CGAL object referred to by receiver. Returns true + // Attempt to parse a JS argument into the CGAL object referred to by receiver. Returns true // if parse was successful, false otherwise. - static bool ParseArg(v8::Isolate *isolate, v8::Local arg, Segment_2 &receiver); + static bool ParseArg(Napi::Env env, Napi::Value arg, Segment_2& receiver); - // Convert a CGAL object of the wrapped class to a POD v8 object. If precise is set to false, - // will attempt to render in terms of doubles for coordinates, and may lose precision. - static v8::Local ToPOD(v8::Isolate *isolate, const Segment_2 &segment, bool precise=true); + // Convert our wrapped CGAL object to a POD JS object. If precise is set to false, + // attempt to render in terms of doubles for coordinates, which may lose precision. + static Napi::Value ToPOD(Napi::Env env, Segment_2 const& segment, bool precise); private: // - //----- The following methods will be callable from JS. These will mostly match + //----- The following methods will be callable from JS. These will mostly match // the semantics and names of the wrapped CGAL class. // - static void IsHorizontal(const v8::FunctionCallbackInfo &info); - static void IsVertical(const v8::FunctionCallbackInfo &info); + Napi::Value IsHorizontal(Napi::CallbackInfo const& info); + Napi::Value IsVertical(Napi::CallbackInfo const& info); }; diff --git a/src/cgal.cc b/src/cgal.cc index 692b30e..3c2e3f1 100644 --- a/src/cgal.cc +++ b/src/cgal.cc @@ -13,7 +13,7 @@ // #include "PolygonSet2.h" // #include "PolygonWithHoles2.h" // #include "Ray2.h" -// #include "Segment2.h" +#include "Segment2.h" // #include "Vector2.h" using namespace std; @@ -61,7 +61,7 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) // PolygonSet2::Init(env, exports); // PolygonWithHoles2::Init(env, exports); // Ray2::Init(env, exports); - // Segment2::Init(env, exports); + Segment2::Init(env, exports); // Vector2::Init(env, exports); return exports;