JSIntegration.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 (auto pos = Sheet::parse_cell_name(name.as_string()); pos.has_value()) {
  45. auto& cell = m_sheet.ensure(pos.value());
  46. cell.reference_from(m_sheet.current_evaluated_cell());
  47. return cell.js_data();
  48. }
  49. }
  50. return GlobalObject::get(name, receiver);
  51. }
  52. bool SheetGlobalObject::put(const JS::PropertyName& name, JS::Value value, JS::Value receiver)
  53. {
  54. if (name.is_string()) {
  55. if (auto pos = Sheet::parse_cell_name(name.as_string()); pos.has_value()) {
  56. auto& cell = m_sheet.ensure(pos.value());
  57. if (auto current = m_sheet.current_evaluated_cell())
  58. current->reference_from(&cell);
  59. cell.set_data(value); // FIXME: This produces un-savable state!
  60. return true;
  61. }
  62. }
  63. return GlobalObject::put(name, value, receiver);
  64. }
  65. void SheetGlobalObject::initialize()
  66. {
  67. GlobalObject::initialize();
  68. define_native_function("parse_cell_name", parse_cell_name, 1);
  69. }
  70. JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::parse_cell_name)
  71. {
  72. if (interpreter.argument_count() != 1) {
  73. interpreter.throw_exception<JS::TypeError>("Expected exactly one argument to parse_cell_name()");
  74. return {};
  75. }
  76. auto name_value = interpreter.argument(0);
  77. if (!name_value.is_string()) {
  78. interpreter.throw_exception<JS::TypeError>("Expected a String argument to parse_cell_name()");
  79. return {};
  80. }
  81. auto position = Sheet::parse_cell_name(name_value.as_string().string());
  82. if (!position.has_value())
  83. return JS::js_undefined();
  84. auto object = JS::Object::create_empty(interpreter.global_object());
  85. object->put("column", JS::js_string(interpreter, position.value().column));
  86. object->put("row", JS::Value((unsigned)position.value().row));
  87. return object;
  88. }
  89. WorkbookObject::WorkbookObject(Workbook& workbook)
  90. : JS::Object(*JS::Object::create_empty(workbook.global_object()))
  91. , m_workbook(workbook)
  92. {
  93. }
  94. WorkbookObject::~WorkbookObject()
  95. {
  96. }
  97. void WorkbookObject::initialize(JS::GlobalObject& global_object)
  98. {
  99. Object::initialize(global_object);
  100. define_native_function("sheet", sheet, 1);
  101. }
  102. JS_DEFINE_NATIVE_FUNCTION(WorkbookObject::sheet)
  103. {
  104. if (interpreter.argument_count() != 1) {
  105. interpreter.throw_exception<JS::TypeError>("Expected exactly one argument to sheet()");
  106. return {};
  107. }
  108. auto name_value = interpreter.argument(0);
  109. if (!name_value.is_string() && !name_value.is_number()) {
  110. interpreter.throw_exception<JS::TypeError>("Expected a String or Number argument to sheet()");
  111. return {};
  112. }
  113. auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
  114. if (!this_object)
  115. return {};
  116. if (!this_object->inherits("WorkbookObject")) {
  117. interpreter.throw_exception<JS::TypeError>(JS::ErrorType::NotA, "WorkbookObject");
  118. return {};
  119. }
  120. auto& workbook = static_cast<WorkbookObject*>(this_object)->m_workbook;
  121. if (name_value.is_string()) {
  122. auto& name = name_value.as_string().string();
  123. for (auto& sheet : workbook.sheets()) {
  124. if (sheet.name() == name)
  125. return JS::Value(&sheet.global_object());
  126. }
  127. } else {
  128. auto index = name_value.as_size_t();
  129. if (index < workbook.sheets().size())
  130. return JS::Value(&workbook.sheets()[index].global_object());
  131. }
  132. return JS::js_undefined();
  133. }
  134. }