|
14 | 14 |
|
15 | 15 | #define THROW_JS_ERROR(errorType, message, ...) \ |
16 | 16 | ({ \ |
17 | | - char msg_buf[128]; \ |
| 17 | + char msg_buf[256]; \ |
18 | 18 | snprintf(msg_buf, sizeof(msg_buf), message, ##__VA_ARGS__); \ |
19 | 19 | EM_ASM(throw new errorType(UTF8ToString($0)), msg_buf); \ |
20 | 20 | __builtin_unreachable(); \ |
@@ -96,18 +96,20 @@ class JsTensor { |
96 | 96 | : tensor_(std::make_shared<Tensor>(tensor)) {} |
97 | 97 |
|
98 | 98 | const Tensor& get_tensor() const { |
| 99 | + THROW_IF_FALSE(tensor_, "Tensor is null"); |
99 | 100 | return *tensor_; |
100 | 101 | } |
101 | 102 |
|
102 | 103 | ScalarType get_scalar_type() const { |
| 104 | + THROW_IF_FALSE(tensor_, "Tensor is null"); |
103 | 105 | return tensor_->scalar_type(); |
104 | 106 | } |
105 | 107 | val get_data() const { |
106 | 108 | switch (get_scalar_type()) { |
107 | | -#define JS_CASE_TENSOR_TO_VAL_TYPE(T, NAME) \ |
108 | | - case ScalarType::NAME: \ |
109 | | - return val( \ |
110 | | - typed_memory_view(get_tensor().numel(), get_tensor().data_ptr<T>())); |
| 109 | +#define JS_CASE_TENSOR_TO_VAL_TYPE(T, NAME) \ |
| 110 | + case ScalarType::NAME: \ |
| 111 | + THROW_IF_FALSE(tensor_->data_ptr<T>(), "Tensor data is null"); \ |
| 112 | + return val(typed_memory_view(tensor_->numel(), tensor_->data_ptr<T>())); |
111 | 113 | JS_FORALL_SUPPORTED_TENSOR_TYPES(JS_CASE_TENSOR_TO_VAL_TYPE) |
112 | 114 | default: |
113 | 115 | THROW_JS_ERROR( |
@@ -246,7 +248,7 @@ val to_val(EValue v) { |
246 | 248 | return val(std::move(wrapper)); |
247 | 249 | } else { |
248 | 250 | char tag_buf[32]; |
249 | | - runtime::tag_to_string(v.tag, tag_buf, 32); |
| 251 | + runtime::tag_to_string(v.tag, tag_buf, sizeof(tag_buf)); |
250 | 252 | THROW_JS_ERROR(TypeError, "Unsupported EValue type: %s", tag_buf); |
251 | 253 | } |
252 | 254 | } |
@@ -340,6 +342,9 @@ class JsModule final { |
340 | 342 | : buffer_(std::move(buffer)), module_(std::move(module)) {} |
341 | 343 |
|
342 | 344 | static std::unique_ptr<JsModule> load(val data) { |
| 345 | + if (data.isNull() || data.isUndefined()) { |
| 346 | + THROW_JS_ERROR(TypeError, "Data cannot be null or undefined"); |
| 347 | + } |
343 | 348 | if (data.isString()) { |
344 | 349 | return std::make_unique<JsModule>( |
345 | 350 | std::make_unique<Module>(data.as<std::string>())); |
|
0 commit comments