Skip to content

Commit 5b315cc

Browse files
galouGaël ÉcorchardomichelCoolSpy3
authored
Add the wbenv JavaScript module (cyberbotics#6793)
* Add the wbenv JavaScript module Add the `wbenv` JavaScript module as a wrapper around C++ functions. Only implements `wbenv.getFromEnv` as of now. Signed-off-by: Gaël Écorchard <gael@km-robotics.cz> * Update the changelog Signed-off-by: Gaël Écorchard <gael@km-robotics.cz> * Satisfy clang-format Signed-off-by: Gaël Écorchard <gael@km-robotics.cz> * Update docs/reference/javascript-procedural-proto.md Co-authored-by: CoolSpy3 <55305038+CoolSpy3@users.noreply.github.com> * Update docs/reference/javascript-procedural-proto.md Co-authored-by: CoolSpy3 <55305038+CoolSpy3@users.noreply.github.com> * Update docs/reference/changelog-r2025.md Co-authored-by: CoolSpy3 <55305038+CoolSpy3@users.noreply.github.com> --------- Signed-off-by: Gaël Écorchard <gael@km-robotics.cz> Co-authored-by: Gaël Écorchard <gael@km-robotics.cz> Co-authored-by: Olivier Michel <Olivier.Michel@cyberbotics.com> Co-authored-by: CoolSpy3 <55305038+CoolSpy3@users.noreply.github.com>
1 parent b23a866 commit 5b315cc

6 files changed

Lines changed: 85 additions & 7 deletions

File tree

docs/reference/changelog-r2025.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Webots R2025 Change Log
22

3+
## Webots R2025b
4+
Released on ??.
5+
- New Features
6+
- Added the `wbenv` Javascript module for procedural protos to get environment variables ([#6793](https://github.com/cyberbotics/webots/pull/6793)).
7+
38
## Webots R2025a
49
Released on January 31st, 2025.
510
- New Features

docs/reference/javascript-procedural-proto.md

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ Sphere {
7070

7171
%end
7272

73-
- Although not mandatory, the usage of semi-colons for JavaScript statements is highly encouraged.
73+
- Although not mandatory, the usage of semicolons for JavaScript statements is highly encouraged.
7474
Which tokens will be considered depends on whether the comment line `# template language: javascript` is present.
75-
- The `wbfile` module for file manipulation does not need to, and should not, be imported as it is added automatically to each instance of the engine.
76-
- Performance degradation has been observed when the number of evaluations requested (i.e expressions of the form `%<= ... >%`) is large, generally in the tens of thousands.
75+
- The modules `wbfile` for file manipulation and `wbenv` for environment variables do not need to, and should not, be imported as they are added automatically to each instance of the engine.
76+
- Performance degradation has been observed when the number of evaluations requested (i.e. expressions of the form `%<= ... >%`) is large, generally in the tens of thousands.
7777
This is typically the case when expressions of this form are used to define the coordinates or indexes of, for instance, a [IndexedFaceSet](indexedfaceset.md).
78-
To greatly speed-up the generation of this sort of PROTO file, it is highly suggested to use a string buffer to which the coordinates are progressively appended and to only evaluate this buffer once at the end, as shown in the following snippet.
78+
To greatly speed up the generation of this sort of PROTO file, it is highly suggested to use a string buffer to which the coordinates are progressively appended and to only evaluate this buffer once at the end, as shown in the following snippet.
7979

8080
%tab-component "generic"
8181

@@ -183,12 +183,14 @@ The available modules are the following:
183183

184184
- `wbutility`: provides commonly needed functions.
185185

186-
Additionally, the following module is automatically loaded to the engine and therefore does not need to be imported:
186+
Additionally, the following modules are automatically loaded to the engine and therefore do not need to be imported:
187187

188188
- `wbfile`: provides functions for the reading and writing of files.
189189

190-
> **Note:**: contrary to the other JavaScript modules, `wbfile` is a C++ wrapped class and therefore cannot and should not be imported manually, attempting to do so will return an error.
191-
The functions exported by this module are available globally.
190+
- `wbenv`: provides functions to get environment variables.
191+
192+
> **Note:**: contrary to the other JavaScript modules, `wbfile` and `wbenv` are C++-wrapped classes and therefore cannot and should not be imported manually, attempting to do so will return an error.
193+
The functions exported by these modules are available globally.
192194

193195
The functions exported by each module are:
194196

@@ -821,6 +823,20 @@ The location of this path can be retrieved from the `context` field object, see
821823

822824
%tab-end
823825

826+
%tab "wbenv"
827+
828+
```javascript
829+
/**
830+
* @param {String} variableName;
831+
* @returns {String}
832+
*/
833+
wbenv.getFromEnv(variableName);
834+
```
835+
836+
Returns the value of the environment variable `variableName` or an empty string if it does not exist.
837+
838+
%tab-end
839+
824840
%end
825841

826842
### PROTO Regeneration

src/webots/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ QT_SOURCES = WbAboutBox.cpp \
380380
WbProtoModel.cpp \
381381
WbProtoTemplateEngine.cpp \
382382
WbProtoTreeItem.cpp \
383+
WbQjsEnv.cpp \
383384
WbQjsFile.cpp \
384385
WbRadar.cpp \
385386
WbRadio.cpp \

src/webots/core/WbQjsEnv.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 1996-2025 Cyberbotics Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "WbQjsEnv.hpp"
16+
17+
#include <cstdlib> // For std::getenv.
18+
19+
QString WbQjsEnv::getFromEnv(const QString &name) const {
20+
return std::getenv(name.toStdString().c_str());
21+
}

src/webots/core/WbQjsEnv.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 1996-2025 Cyberbotics Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef WB_QJS_ENV_HPP
16+
#define WB_QJS_ENV_HPP
17+
18+
#include <QtCore/QObject>
19+
20+
class WbQjsEnv : public QObject {
21+
Q_OBJECT
22+
23+
public:
24+
Q_INVOKABLE WbQjsEnv(){};
25+
~WbQjsEnv(){};
26+
27+
Q_INVOKABLE QString getFromEnv(const QString &name) const;
28+
};
29+
30+
#endif

src/webots/core/WbTemplateEngine.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "WbLog.hpp"
1818
#include "WbProject.hpp"
19+
#include "WbQjsEnv.hpp"
1920
#include "WbQjsFile.hpp"
2021
#include "WbStandardPaths.hpp"
2122

@@ -209,6 +210,10 @@ bool WbTemplateEngine::generateJavascript(QHash<QString, QString> tags, const QS
209210
WbQjsFile *jsFileObject = new WbQjsFile();
210211
QJSValue jsFile = engine.newQObject(jsFileObject);
211212
engine.globalObject().setProperty("wbfile", jsFile);
213+
// create and add environment module
214+
WbQjsEnv *jsEnvObject = new WbQjsEnv();
215+
QJSValue jsEnv = engine.newQObject(jsEnvObject);
216+
engine.globalObject().setProperty("wbenv", jsEnv);
212217
// add stream holders
213218
QJSValue jsStdOut = engine.newArray();
214219
engine.globalObject().setProperty("stdout", jsStdOut);

0 commit comments

Comments
 (0)