JSIntegration.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "JSIntegration.h"
  27. #include "Spreadsheet.h"
  28. #include "Workbook.h"
  29. #include <LibJS/Runtime/Error.h>
  30. #include <LibJS/Runtime/GlobalObject.h>
  31. #include <LibJS/Runtime/Object.h>
  32. #include <LibJS/Runtime/Value.h>
  33. namespace Spreadsheet {
  34. SheetGlobalObject::SheetGlobalObject(Sheet& sheet)
  35. : m_sheet(sheet)
  36. {
  37. }
  38. SheetGlobalObject::~SheetGlobalObject()
  39. {
  40. }
  41. JS::Value SheetGlobalObject::get(const JS::PropertyName& name, JS::Value receiver) const
  42. {
  43. if (name.is_string()) {
  44. if (name.as_string() == "value") {
  45. if (auto cell = m_sheet.current_evaluated_cell())
  46. return cell->js_data();
  47. return JS::js_undefined();
  48. }
  49. if (auto pos = Sheet::parse_cell_name(name.as_string()); pos.has_value()) {
  50. auto& cell = m_sheet.ensure(pos.value());
  51. cell.reference_from(m_sheet.current_evaluated_cell());
  52. return cell.typed_js_data();
  53. }
  54. }
  55. return GlobalObject::get(name, receiver);
  56. }
  57. bool SheetGlobalObject::put(const JS::PropertyName& name, JS::Value value, JS::Value receiver)
  58. {
  59. if (name.is_string()) {
  60. if (auto pos = Sheet::parse_cell_name(name.as_string()); pos.has_value()) {
  61. auto& cell = m_sheet.ensure(pos.value());
  62. if (auto current = m_sheet.current_evaluated_cell())
  63. current->reference_from(&cell);
  64. cell.set_data(value); // FIXME: This produces un-savable state!
  65. return true;
  66. }
  67. }
  68. return GlobalObject::put(name, value, receiver);
  69. }
  70. void SheetGlobalObject::initialize()
  71. {
  72. GlobalObject::initialize();
  73. define_native_function("parse_cell_name", parse_cell_name, 1);
  74. }
  75. JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::parse_cell_name)
  76. {
  77. if (interpreter.argument_count() != 1) {
  78. interpreter.vm().throw_exception<JS::TypeError>(global_object, "Expected exactly one argument to parse_cell_name()");
  79. return {};
  80. }
  81. auto name_value = interpreter.argument(0);
  82. if (!name_value.is_string()) {
  83. interpreter.vm().throw_exception<JS::TypeError>(global_object, "Expected a String argument to parse_cell_name()");
  84. return {};
  85. }
  86. auto position = Sheet::parse_cell_name(name_value.as_string().string());
  87. if (!position.has_value())
  88. return JS::js_undefined();
  89. auto object = JS::Object::create_empty(interpreter.global_object());
  90. object->put("column", JS::js_string(interpreter, position.value().column));
  91. object->put("row", JS::Value((unsigned)position.value().row));
  92. return object;
  93. }
  94. WorkbookObject::WorkbookObject(Workbook& workbook)
  95. : JS::Object(*JS::Object::create_empty(workbook.global_object()))
  96. , m_workbook(workbook)
  97. {
  98. }
  99. WorkbookObject::~WorkbookObject()
  100. {
  101. }
  102. void WorkbookObject::initialize(JS::GlobalObject& global_object)
  103. {
  104. Object::initialize(global_object);
  105. define_native_function("sheet", sheet, 1);
  106. }
  107. JS_DEFINE_NATIVE_FUNCTION(WorkbookObject::sheet)
  108. {
  109. if (interpreter.argument_count() != 1) {
  110. interpreter.vm().throw_exception<JS::TypeError>(global_object, "Expected exactly one argument to sheet()");
  111. return {};
  112. }
  113. auto name_value = interpreter.argument(0);
  114. if (!name_value.is_string() && !name_value.is_number()) {
  115. interpreter.vm().throw_exception<JS::TypeError>(global_object, "Expected a String or Number argument to sheet()");
  116. return {};
  117. }
  118. auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
  119. if (!this_object)
  120. return {};
  121. if (!this_object->inherits("WorkbookObject")) {
  122. interpreter.vm().throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "WorkbookObject");
  123. return {};
  124. }
  125. auto& workbook = static_cast<WorkbookObject*>(this_object)->m_workbook;
  126. if (name_value.is_string()) {
  127. auto& name = name_value.as_string().string();
  128. for (auto& sheet : workbook.sheets()) {
  129. if (sheet.name() == name)
  130. return JS::Value(&sheet.global_object());
  131. }
  132. } else {
  133. auto index = name_value.as_size_t();
  134. if (index < workbook.sheets().size())
  135. return JS::Value(&workbook.sheets()[index].global_object());
  136. }
  137. return JS::js_undefined();
  138. }
  139. }