#include #include "boolean.h" namespace SassTypes { Nan::Persistent Boolean::constructor; bool Boolean::constructor_locked = false; Boolean::Boolean(bool v) : value(v) {} Boolean& Boolean::get_singleton(bool v) { static Boolean instance_false(false), instance_true(true); return v ? instance_true : instance_false; } v8::Local Boolean::get_constructor() { Nan::EscapableHandleScope scope; v8::Local conslocal; if (constructor.IsEmpty()) { v8::Local tpl = Nan::New(New); tpl->SetClassName(Nan::New("SassBoolean").ToLocalChecked()); tpl->InstanceTemplate()->SetInternalFieldCount(1); Nan::SetPrototypeTemplate(tpl, "getValue", Nan::New(GetValue)); conslocal = Nan::GetFunction(tpl).ToLocalChecked(); constructor.Reset(conslocal); get_singleton(false).js_object.Reset(Nan::NewInstance(conslocal).ToLocalChecked()); Nan::SetInternalFieldPointer(Nan::New(get_singleton(false).js_object), 0, &get_singleton(false)); Nan::Set(conslocal, Nan::New("FALSE").ToLocalChecked(), Nan::New(get_singleton(false).js_object)); get_singleton(true).js_object.Reset(Nan::NewInstance(conslocal).ToLocalChecked()); Nan::SetInternalFieldPointer(Nan::New(get_singleton(true).js_object), 0, &get_singleton(true)); Nan::Set(conslocal, Nan::New("TRUE").ToLocalChecked(), Nan::New(get_singleton(true).js_object)); constructor_locked = true; } else { conslocal = Nan::New(constructor); } return scope.Escape(conslocal); } Sass_Value* Boolean::get_sass_value() { return sass_make_boolean(value); } v8::Local Boolean::get_js_object() { return Nan::New(this->js_object); } NAN_METHOD(Boolean::New) { if (info.IsConstructCall()) { if (constructor_locked) { return Nan::ThrowTypeError("Cannot instantiate SassBoolean"); } } else { if (info.Length() != 1 || !info[0]->IsBoolean()) { return Nan::ThrowTypeError("Expected one boolean argument"); } info.GetReturnValue().Set(get_singleton(Nan::To(info[0]).FromJust()).get_js_object()); } } NAN_METHOD(Boolean::GetValue) { Boolean *out; if ((out = static_cast(Factory::unwrap(info.This())))) { info.GetReturnValue().Set(Nan::New(out->value)); } } }