CPP   18

map.cpp

Guest on 20th May 2022 01:38:44 AM

  1. #include <nan.h>
  2. #include "map.h"
  3.  
  4. namespace SassTypes
  5. {
  6.   Map::Map(Sass_Value* v) : SassValueWrapper(v) {}
  7.  
  8.   Sass_Value* Map::construct(const std::vector<v8::Local<v8::Value>> raw_val, Sass_Value **out) {
  9.     size_t length = 0;
  10.  
  11.     if (raw_val.size() >= 1) {
  12.       if (!raw_val[0]->IsNumber()) {
  13.         return fail("First argument should be an integer.", out);
  14.       }
  15.  
  16.       length = Nan::To<uint32_t>(raw_val[0]).FromJust();
  17.     }
  18.  
  19.     return *out = sass_make_map(length);
  20.   }
  21.  
  22.   void Map::initPrototype(v8::Local<v8::FunctionTemplate> proto) {
  23.     Nan::SetPrototypeMethod(proto, "getLength", GetLength);
  24.     Nan::SetPrototypeMethod(proto, "getKey", GetKey);
  25.     Nan::SetPrototypeMethod(proto, "setKey", SetKey);
  26.     Nan::SetPrototypeMethod(proto, "getValue", GetValue);
  27.     Nan::SetPrototypeMethod(proto, "setValue", SetValue);
  28.   }
  29.  
  30.   NAN_METHOD(Map::GetValue) {
  31.  
  32.     if (info.Length() != 1) {
  33.       return Nan::ThrowTypeError("Expected just one argument");
  34.     }
  35.  
  36.     if (!info[0]->IsNumber()) {
  37.       return Nan::ThrowTypeError("Supplied index should be an integer");
  38.     }
  39.  
  40.     Sass_Value* map = Map::Unwrap<Map>(info.This())->value;
  41.     size_t index = Nan::To<uint32_t>(info[0]).FromJust();
  42.  
  43.  
  44.     if (index >= sass_map_get_length(map)) {
  45.       return Nan::ThrowRangeError(Nan::New("Out of bound index").ToLocalChecked());
  46.     }
  47.  
  48.     info.GetReturnValue().Set(Factory::create(sass_map_get_value(map, Nan::To<uint32_t>(info[0]).FromJust()))->get_js_object());
  49.   }
  50.  
  51.   NAN_METHOD(Map::SetValue) {
  52.     if (info.Length() != 2) {
  53.       return Nan::ThrowTypeError("Expected two arguments");
  54.     }
  55.  
  56.     if (!info[0]->IsNumber()) {
  57.       return Nan::ThrowTypeError("Supplied index should be an integer");
  58.     }
  59.  
  60.     if (!info[1]->IsObject()) {
  61.       return Nan::ThrowTypeError("Supplied value should be a SassValue object");
  62.     }
  63.  
  64.     Value* sass_value = Factory::unwrap(info[1]);
  65.     if (sass_value) {
  66.       sass_map_set_value(Map::Unwrap<Map>(info.This())->value, Nan::To<uint32_t>(info[0]).FromJust(), sass_value->get_sass_value());
  67.     } else {
  68.       Nan::ThrowTypeError("A SassValue is expected as a map value");
  69.     }
  70.   }
  71.  
  72.   NAN_METHOD(Map::GetKey) {
  73.     if (info.Length() != 1) {
  74.       return Nan::ThrowTypeError("Expected just one argument");
  75.     }
  76.  
  77.     if (!info[0]->IsNumber()) {
  78.       return Nan::ThrowTypeError("Supplied index should be an integer");
  79.     }
  80.  
  81.     Sass_Value* map = Map::Unwrap<Map>(info.This())->value;
  82.     size_t index = Nan::To<uint32_t>(info[0]).FromJust();
  83.  
  84.  
  85.     if (index >= sass_map_get_length(map)) {
  86.       return Nan::ThrowRangeError(Nan::New("Out of bound index").ToLocalChecked());
  87.     }
  88.  
  89.     SassTypes::Value* obj = Factory::create(sass_map_get_key(map, Nan::To<uint32_t>(info[0]).FromJust()));
  90.     v8::Local<v8::Object> js_obj = obj->get_js_object();
  91.     info.GetReturnValue().Set(js_obj);
  92.   }
  93.  
  94.   NAN_METHOD(Map::SetKey) {
  95.     if (info.Length() != 2) {
  96.       return Nan::ThrowTypeError("Expected two arguments");
  97.     }
  98.  
  99.     if (!info[0]->IsNumber()) {
  100.       return Nan::ThrowTypeError("Supplied index should be an integer");
  101.     }
  102.  
  103.     if (!info[1]->IsObject()) {
  104.       return Nan::ThrowTypeError("Supplied value should be a SassValue object");
  105.     }
  106.  
  107.     Value* sass_value = Factory::unwrap(info[1]);
  108.     if (sass_value) {
  109.       sass_map_set_key(Map::Unwrap<Map>(info.This())->value, Nan::To<uint32_t>(info[0]).FromJust(), sass_value->get_sass_value());
  110.     } else {
  111.       Nan::ThrowTypeError("A SassValue is expected as a map key");
  112.     }
  113.   }
  114.  
  115.   NAN_METHOD(Map::GetLength) {
  116.     info.GetReturnValue().Set(Nan::New<v8::Number>(sass_map_get_length(Map::Unwrap<Map>(info.This())->value)));
  117.   }
  118. }

Raw Paste


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