CPP   53

factory.cpp

Guest on 20th May 2022 01:42:08 AM

  1. #include <nan.h>
  2. #include "factory.h"
  3. #include "value.h"
  4. #include "number.h"
  5. #include "string.h"
  6. #include "color.h"
  7. #include "boolean.h"
  8. #include "list.h"
  9. #include "map.h"
  10. #include "null.h"
  11. #include "error.h"
  12.  
  13. namespace SassTypes
  14. {
  15.   SassTypes::Value* Factory::create(Sass_Value* v) {
  16.     switch (sass_value_get_tag(v)) {
  17.     case SASS_NUMBER:
  18.       return new Number(v);
  19.  
  20.     case SASS_STRING:
  21.       return new String(v);
  22.  
  23.     case SASS_COLOR:
  24.       return new Color(v);
  25.  
  26.     case SASS_BOOLEAN:
  27.       return &Boolean::get_singleton(sass_boolean_get_value(v));
  28.  
  29.     case SASS_LIST:
  30.       return new List(v);
  31.  
  32.     case SASS_MAP:
  33.       return new Map(v);
  34.  
  35.     case SASS_NULL:
  36.       return &Null::get_singleton();
  37.  
  38.     case SASS_ERROR:
  39.       return new Error(v);
  40.  
  41.     default:
  42.       const char *msg = "Unknown type encountered.";
  43.       Nan::ThrowTypeError(msg);
  44.       return new Error(sass_make_error(msg));
  45.     }
  46.   }
  47.  
  48.   NAN_MODULE_INIT(Factory::initExports) {
  49.     Nan::HandleScope scope;
  50.     v8::Local<v8::Object> types = Nan::New<v8::Object>();
  51.  
  52.     Nan::Set(types, Nan::New("Number").ToLocalChecked(), Number::get_constructor());
  53.     Nan::Set(types, Nan::New("String").ToLocalChecked(), String::get_constructor());
  54.     Nan::Set(types, Nan::New("Color").ToLocalChecked(), Color::get_constructor());
  55.     Nan::Set(types, Nan::New("Boolean").ToLocalChecked(), Boolean::get_constructor());
  56.     Nan::Set(types, Nan::New("List").ToLocalChecked(), List::get_constructor());
  57.     Nan::Set(types, Nan::New("Map").ToLocalChecked(), Map::get_constructor());
  58.     Nan::Set(types, Nan::New("Null").ToLocalChecked(), Null::get_constructor());
  59.     Nan::Set(types, Nan::New("Error").ToLocalChecked(), Error::get_constructor());
  60.     Nan::Set(target, Nan::New<v8::String>("types").ToLocalChecked(), types);
  61.   }
  62.  
  63.   Value* Factory::unwrap(v8::Local<v8::Value> obj) {
  64.       if (obj->IsObject()) {
  65.           v8::Local<v8::Object> v8_obj = obj.As<v8::Object>();
  66.           if (v8_obj->InternalFieldCount() == 1) {
  67.               return SassTypes::Value::Unwrap<Value>(v8_obj);
  68.           }
  69.       }
  70.       return NULL;
  71.   }
  72. }

Raw Paste


Login or Register to edit or fork this paste. It's free.