Skip to content

Commit 1bddf40

Browse files
authored
Merge pull request #1 from think-cell/minor_fixes
Minor fixes by Sebastian
2 parents 37a5366 + ed65fed commit 1bddf40

File tree

5 files changed

+84
-50
lines changed

5 files changed

+84
-50
lines changed

bootstrap/include/js_bootstrap.h

+58-39
Original file line numberDiff line numberDiff line change
@@ -19,83 +19,84 @@ template<typename> struct _js_ReadonlyArray;
1919
template<typename> struct _js_Promise;
2020
struct _js_console;
2121

22-
template<typename T> using Array = jst::js_ref<_js_Array<T>>;
23-
template<typename T> using ReadonlyArray = jst::js_ref<_js_ReadonlyArray<T>>;
24-
template<typename T> using Promise = jst::js_ref<_js_Promise<T>>;
25-
using console = jst::js_ref<_js_console>;
22+
template<typename T> using Array = ::tc::jst::js_ref<_js_Array<T>>;
23+
template<typename T> using ReadonlyArray = ::tc::jst::js_ref<_js_ReadonlyArray<T>>;
24+
template<typename T> using Promise = ::tc::jst::js_ref<_js_Promise<T>>;
25+
using console = ::tc::jst::js_ref<_js_console>;
2626

2727
template<typename T>
28-
struct _js_Array : virtual jst::IObject {
29-
static_assert(jst::IsJsInteropable<T>::value);
28+
struct _js_Array : virtual ::tc::jst::IObject {
29+
static_assert(::tc::jst::IsJsInteropable<T>::value);
3030

3131
struct _tcjs_definitions {
3232
using value_type = T;
3333
};
3434

35-
auto length() noexcept { return tc::explicit_cast<int>(_getProperty<double>("length")); }
35+
auto length() noexcept { return ::tc::explicit_cast<int>(_getProperty<double>("length")); }
3636

3737
auto push(T const& item) noexcept { return _call<void>("push", item); }
3838

39-
auto operator[](int i) && noexcept { return _getProperty<T>(tc::as_dec(i)); }
39+
auto operator[](int i) && noexcept { return _getProperty<T>(::tc::as_dec(i)); }
4040

41-
void _setIndex(int i, T value) noexcept { _setProperty(tc::as_dec(i), tc_move(value)); }
41+
void _setIndex(int i, T value) noexcept { _setProperty(::tc::as_dec(i), tc_move(value)); }
4242

4343
// Generator range. This adds operator() to array interface (which did not exist before), but it's ok.
4444
template<typename Fn>
4545
auto operator()(Fn fn) noexcept {
46-
if (_call<bool>("some", jst::js_lambda_wrap([&](T value, jst::js_unknown, jst::js_unknown) noexcept {
47-
return tc::break_ == tc::continue_if_not_break(fn, tc_move(value));
46+
if (_call<bool>("some", ::tc::jst::js_lambda_wrap([&](T value, ::tc::jst::js_unknown, ::tc::jst::js_unknown) noexcept {
47+
return ::tc::break_ == tc::continue_if_not_break(fn, tc_move(value));
4848
})))
49-
return tc::break_;
49+
return ::tc::break_;
5050
else
51-
return tc::continue_;
51+
return ::tc::continue_;
5252
}
5353

5454
static Array<T> _tcjs_construct() noexcept {
55-
return Array<T>(emscripten::val::array());
55+
return Array<T>(::emscripten::val::array());
5656
}
5757

58-
template<typename Rng, typename = std::enable_if_t<tc::is_explicit_castable<T, tc::range_value_t<Rng>&&>::value>>
58+
template<typename Rng, typename = ::std::enable_if_t<::tc::is_explicit_castable<T, ::tc::range_reference_t<Rng>>::value>>
5959
static Array<T> _tcjs_construct(Rng&& rng) noexcept {
60-
Array<T> result(emscripten::val::array());
61-
tc::for_each(rng, [&](auto&& value) noexcept {
62-
result->push(tc::explicit_cast<T>(std::forward<decltype(value)>(value)));
60+
Array<T> result(::emscripten::val::array());
61+
::tc::for_each(::std::forward<Rng>(rng), [&](auto&& value) noexcept {
62+
result->push(::tc::explicit_cast<T>(::std::forward<decltype(value)>(value)));
6363
});
6464
return result;
6565
}
66+
6667
};
6768

6869
template<typename T>
69-
struct _js_ReadonlyArray : virtual jst::IObject {
70-
static_assert(jst::IsJsInteropable<T>::value);
70+
struct _js_ReadonlyArray : virtual ::tc::jst::IObject {
71+
static_assert(::tc::jst::IsJsInteropable<T>::value);
7172

7273
struct _tcjs_definitions {
7374
using value_type = T;
7475
};
7576

76-
auto length() noexcept { return tc::explicit_cast<int>(_getProperty<double>("length")); }
77+
auto length() noexcept { return ::tc::explicit_cast<int>(_getProperty<double>("length")); }
7778

78-
auto operator[](int i) noexcept { return _getProperty<T>(tc::as_dec(i)); }
79+
auto operator[](int i) noexcept { return _getProperty<T>(::tc::as_dec(i)); }
7980

8081
// Generator range. This adds operator() to array interface (which did not exist before), but it's ok.
8182
template<typename Fn>
8283
auto operator()(Fn fn) noexcept {
83-
if (_call<bool>("some", jst::js_lambda_wrap([&](T value, jst::js_unknown, jst::js_unknown) noexcept {
84-
return tc::break_ == tc::continue_if_not_break(fn, tc_move(value));
84+
if (_call<bool>("some", ::tc::jst::js_lambda_wrap([&](T value, ::tc::jst::js_unknown, ::tc::jst::js_unknown) noexcept {
85+
return ::tc::break_ == ::tc::continue_if_not_break(fn, tc_move(value));
8586
})))
86-
return tc::break_;
87+
return ::tc::break_;
8788
else
88-
return tc::continue_;
89+
return ::tc::continue_;
8990
}
9091

9192
static ReadonlyArray<T> _tcjs_construct() noexcept {
92-
return ReadonlyArray<T>(emscripten::val::array());
93+
return ReadonlyArray<T>(::emscripten::val::array());
9394
}
9495

95-
template<typename Rng, typename = std::enable_if_t<tc::is_explicit_castable<T, tc::range_value_t<Rng>&&>::value>>
96+
template<typename Rng, typename = ::std::enable_if_t<::tc::is_explicit_castable<T, ::tc::range_reference_t<Rng>>::value>>
9697
static ReadonlyArray<T> _tcjs_construct(Rng&& rng) noexcept {
9798
return ReadonlyArray<T>(
98-
Array<T>(jst::create_js_object, std::forward<Rng>(rng)).getEmval()
99+
Array<T>(::tc::jst::create_js_object, ::std::forward<Rng>(rng)).getEmval()
99100
);
100101
}
101102
};
@@ -105,36 +106,54 @@ template<typename T> struct RemovePromise<Promise<T>> { using type = T; };
105106
template<typename T> using RemovePromise_t = typename RemovePromise<T>::type;
106107

107108
template<typename T>
108-
struct _js_Promise : virtual jst::IObject {
109-
static_assert(jst::IsJsInteropable<T>::value);
109+
struct _js_Promise : virtual ::tc::jst::IObject {
110+
static_assert(::tc::jst::IsJsInteropable<T>::value);
110111

111112
template<typename R>
112-
auto then(jst::js_function<R(T)> onfulfilled, jst::js_function<R(jst::js_unknown)> onrejected) noexcept {
113+
auto then(::tc::jst::js_function<R(T)> onfulfilled, ::tc::jst::js_function<R(::tc::jst::js_unknown)> onrejected) noexcept {
113114
return _call<Promise<RemovePromise_t<R>>>("then", onfulfilled, onrejected);
114115
}
115116

116117
template<typename R1>
117-
auto then(jst::js_function<R1(T)> onfulfilled) noexcept {
118+
auto then(::tc::jst::js_function<R1(T)> onfulfilled) noexcept {
118119
return _call<Promise<RemovePromise_t<R1>>>("then", onfulfilled);
119120
}
120121

121122
template<typename R1, typename R2>
122-
auto then(jst::js_function<R1(T)> onfulfilled, jst::js_function<R2(jst::js_unknown)> onrejected) noexcept {
123-
return _call<Promise<jst::js_union<RemovePromise_t<R1>, RemovePromise_t<R2>>>>("then", onfulfilled, onrejected);
123+
auto then(::tc::jst::js_function<R1(T)> onfulfilled, ::tc::jst::js_function<R2(jst::js_unknown)> onrejected) noexcept {
124+
return _call<Promise<::tc::jst::js_union<RemovePromise_t<R1>, RemovePromise_t<R2>>>>("then", onfulfilled, onrejected);
124125
}
125126
};
126127

127128
template<>
128-
struct _js_Promise<void> : virtual _js_Promise<jst::js_undefined> {
129+
struct _js_Promise<void> : virtual _js_Promise<::tc::jst::js_undefined> {
129130
// JavaScript passes 'undefined' to what TypeScript calls 'void' promise.
130131
};
131132

132-
struct _js_console : virtual jst::IObject {
133+
struct _js_console : virtual ::tc::jst::IObject {
133134
struct _tcjs_definitions {
134135
template<typename... Args>
135136
static void log(Args&&... args) noexcept {
136-
static_assert((jst::IsJsInteropable<tc::remove_cvref_t<Args>>::value && ...));
137-
emscripten::val::global("console")["log"](std::forward<Args>(args)...);
137+
static_assert((::tc::jst::IsJsInteropable<::tc::remove_cvref_t<Args>>::value && ...));
138+
::emscripten::val::global("console")["log"](::std::forward<Args>(args)...);
139+
}
140+
141+
template<typename... Args>
142+
static void error(Args&&... args) noexcept {
143+
static_assert((::tc::jst::IsJsInteropable<::tc::remove_cvref_t<Args>>::value && ...));
144+
::emscripten::val::global("console")["error"](::std::forward<Args>(args)...);
145+
}
146+
147+
template<typename... Args>
148+
static void warn(Args&&... args) noexcept {
149+
static_assert((::tc::jst::IsJsInteropable<::tc::remove_cvref_t<Args>>::value && ...));
150+
::emscripten::val::global("console")["warn"](::std::forward<Args>(args)...);
151+
}
152+
153+
template<typename... Args>
154+
static void debug(Args&&... args) noexcept {
155+
static_assert((::tc::jst::IsJsInteropable<::tc::remove_cvref_t<Args>>::value && ...));
156+
::emscripten::val::global("console")["debug"](::std::forward<Args>(args)...);
138157
}
139158
};
140159
};

bootstrap/include/js_callback.h

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
#pragma once
2-
#include <emscripten/val.h>
3-
#include <emscripten/wire.h>
4-
#include <cstdint>
5-
#include <memory>
6-
#include <utility>
7-
#include <type_traits>
8-
92
#include "casts.h"
3+
104
#include "range_defines.h"
115
#include "type_traits.h"
126
#include "noncopyable.h"
137
#include "tag_type.h"
148
#include "tc_move.h"
159
#include "type_list.h"
1610
#include <boost/callable_traits.hpp>
11+
12+
#include <emscripten/val.h>
13+
#include <emscripten/wire.h>
14+
#include <cstdint>
15+
#include <memory>
16+
#include <utility>
17+
#include <type_traits>
18+
1719
#include "js_ref.h"
1820

1921
namespace tc::jst {

bootstrap/src/js_callback.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include <emscripten/bind.h>
21
#include "js_callback.h"
2+
#include <emscripten/bind.h>
33

44
namespace tc::jst {
55
namespace callback_detail {
@@ -9,8 +9,8 @@ void EnsureJsCallbackCppIsLinked() {}
99
* [basic.compound] 6.7.2
1010
* (3) The type of a pointer to cv void or a pointer to an object type is called an object pointer type. ...
1111
* ... The type of a pointer that can designate a function is called a function pointer type. A pointer to
12-
* objects of type T is referred to as a pointer to T. ... Except for pointers to static members, text
13-
* referring to pointers does not apply to pointers to members.
12+
* objects of type T is referred to as a "pointer to T". ... Except for pointers to static members, text
13+
* referring to "pointers" does not apply to pointers to members.
1414
*
1515
* [expr.reinterpret.cast] 8.5.1.10
1616
* (1) ... Conversions that can be performed explicitly using reinterpret_cast are listed below. ...
@@ -20,7 +20,7 @@ void EnsureJsCallbackCppIsLinked() {}
2020
* an integer of sufficient size (if any such exists on the implementation) and back to the same pointer type will
2121
* have its original value; mappings between pointers and integers are otherwise implementation-defined. [ Note:
2222
* Except as described in 6.6.4.4.3, the result of such a conversion will not be a safely-derived pointer value.
23-
* end note ]
23+
* [end note]
2424
*
2525
* Hence, I conclude that converting between std::uintptr_t and non-member/static function pointers via reinterpret_cast
2626
* is safe as long as PointerNumber is "large enough", which is _hopefully_ checked by sizeof().

examples/BootstrapDemo/main.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ int main() {
6262
_ASSERT(!tc::empty(arr));
6363
}
6464

65+
{
66+
constexpr char str[] = "Test";
67+
Array<js_string> const arr(tc::jst::create_js_object, tc::single(str));
68+
_ASSERTEQUAL(arr->length(), 1);
69+
_ASSERTEQUAL(arr[0].length(), 4);
70+
}
71+
{
72+
constexpr char str[] = "Test";
73+
ReadonlyArray<js_string> const arr(tc::jst::create_js_object, tc::single(str));
74+
_ASSERTEQUAL(arr->length(), 1);
75+
_ASSERTEQUAL(arr[0].length(), 4);
76+
}
77+
6578
auto const arr = tc::explicit_cast<Array<double>>(tc::jst::create_js_object, std::initializer_list<double>{1, 2, 3});
6679
static_assert(!tc::is_explicit_castable<Array<double>, double>::value);
6780
static_assert(std::is_same_v<tc::range_value_t<Array<double>>, double>);

stage1/build-make.sh

100644100755
File mode changed.

0 commit comments

Comments
 (0)