Skip to content

Commit 6b75d8f

Browse files
Merge pull request #362 from Distributive-Network/Xmader/fix/pmjs-rval
PMJS should run files in script mode, shouldn't try to retrieve the last expression value of the file
2 parents c0af5ff + 2462173 commit 6b75d8f

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,11 @@ They are largely based on SpiderMonkey's `CompileOptions`. The supported option
164164
- `filename`: set the filename of this code for the purposes of generating stack traces etc.
165165
- `lineno`: set the line number offset of this code for the purposes of generating stack traces etc.
166166
- `column`: set the column number offset of this code for the purposes of generating stack traces etc.
167-
- `mutedErrors`: experimental
168-
- `noScriptRval`: experimental
169-
- `selfHosting`: experimental
170-
- `strict`: experimental
171-
- `module`: experimental
167+
- `mutedErrors`: if set to `True`, eval errors or unhandled rejections are ignored ("muted"). Default `False`.
168+
- `noScriptRval`: if `False`, return the last expression value of the script as the result value to the caller. Default `False`.
169+
- `selfHosting`: *experimental*
170+
- `strict`: forcibly evaluate in strict mode (`"use strict"`). Default `False`.
171+
- `module`: indicate the file is an ECMAScript module (always strict mode code and disallow HTML comments). Default `False`.
172172
- `fromPythonFrame`: generate the equivalent of filename, lineno, and column based on the location of
173173
the Python call to eval. This makes it possible to evaluate Python multiline string literals and
174174
generate stack traces in JS pointing to the error in the Python source file.

python/pythonmonkey/require.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,8 @@ def runProgramModule(filename, argv, extraPaths=[]):
409409
globalThis.__filename = fullFilename
410410
globalThis.__dirname = os.path.dirname(fullFilename)
411411
with open(fullFilename, encoding="utf-8", mode="r") as mainModuleSource:
412-
pm.eval(mainModuleSource.read(), {'filename': fullFilename})
412+
pm.eval(mainModuleSource.read(), {'filename': fullFilename, 'noScriptRval': True})
413+
# forcibly run in file mode. We shouldn't be getting the last expression of the script as the result value.
413414

414415
# The pythonmonkey require export. Every time it is used, the stack is inspected so that the filename
415416
# passed to createRequire is correct. This is necessary so that relative requires work. If the filename

src/pyTypeFactory.cc

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
#include <js/ValueArray.h>
3838

3939
PyObject *pyTypeFactory(JSContext *cx, JS::HandleValue rval) {
40+
std::string errorString;
41+
4042
if (rval.isUndefined()) {
4143
return NoneType::getPyObject();
4244
}
@@ -53,7 +55,7 @@ PyObject *pyTypeFactory(JSContext *cx, JS::HandleValue rval) {
5355
return StrType::getPyObject(cx, rval);
5456
}
5557
else if (rval.isSymbol()) {
56-
printf("symbol type is not handled by PythonMonkey yet");
58+
errorString = "symbol type is not handled by PythonMonkey yet.\n";
5759
}
5860
else if (rval.isBigInt()) {
5961
return IntType::getPyObject(cx, rval.toBigInt());
@@ -117,12 +119,17 @@ PyObject *pyTypeFactory(JSContext *cx, JS::HandleValue rval) {
117119
return DictType::getPyObject(cx, rval);
118120
}
119121
else if (rval.isMagic()) {
120-
printf("magic type is not handled by PythonMonkey yet\n");
122+
errorString = "magic type is not handled by PythonMonkey yet.\n";
121123
}
122124

123-
std::string errorString("pythonmonkey cannot yet convert Javascript value of: ");
124-
JS::RootedString str(cx, JS::ToString(cx, rval));
125-
errorString += JS_EncodeStringToUTF8(cx, str).get();
125+
errorString += "pythonmonkey cannot yet convert Javascript value of: ";
126+
JSString *valToStr = JS::ToString(cx, rval);
127+
if (!valToStr) { // `JS::ToString` returns `nullptr` for JS symbols, see https://hg.mozilla.org/releases/mozilla-esr102/file/3b574e1/js/src/vm/StringType.cpp#l2208
128+
// TODO (Tom Tang): Revisit this once we have Symbol coercion support
129+
valToStr = JS_ValueToSource(cx, rval);
130+
}
131+
JS::RootedString valToStrRooted(cx, valToStr);
132+
errorString += JS_EncodeStringToUTF8(cx, valToStrRooted).get();
126133
PyErr_SetString(PyExc_TypeError, errorString.c_str());
127134
return NULL;
128135
}

0 commit comments

Comments
 (0)