new safe extensions mechanism

snap7
jmoenig 2021-06-14 13:04:25 +02:00
rodzic e20517d83f
commit 975145cede
6 zmienionych plików z 162 dodań i 14 usunięć

Wyświetl plik

@ -4,6 +4,7 @@
### 2021-06-14
* new dev version
* threads, blocks, objects, extensions: new safe extensions mechanism
## 6.9.0
* **Notable Changes:**

Wyświetl plik

@ -8,9 +8,9 @@
<script src="src/morphic.js?version=2021-02-10"></script>
<script src="src/symbols.js?version=2021-03-03"></script>
<script src="src/widgets.js?version=2021-01-05"></script>
<script src="src/blocks.js?version=2021-06-11"></script>
<script src="src/threads.js?version=2021-06-10"></script>
<script src="src/objects.js?version=2021-06-09"></script>
<script src="src/blocks.js?version=2021-06-14"></script>
<script src="src/threads.js?version=2021-06-14"></script>
<script src="src/objects.js?version=2021-06-14"></script>
<script src="src/gui.js?version=2021-06-14"></script>
<script src="src/paint.js?version=2020-05-17"></script>
<script src="src/lists.js?version=2021-03-15"></script>
@ -19,6 +19,7 @@
<script src="src/sketch.js?version=2020-07-13"></script>
<script src="src/video.js?version=2019-06-27"></script>
<script src="src/maps.js?version=2020-03-25"></script>
<script src="src/extensions.js?version=2021-06-14"></script>
<script src="src/xml.js?version=2020-04-27"></script>
<script src="src/store.js?version=2021-06-10"></script>
<script src="src/locale.js?version=2021-06-11"></script>

Wyświetl plik

@ -146,19 +146,19 @@
/*global Array, BoxMorph,
Color, ColorPaletteMorph, FrameMorph, Function, HandleMorph, Math, MenuMorph,
Morph, MorphicPreferences, Object, Point, ScrollFrameMorph, ShadowMorph, ZERO,
String, StringMorph, TextMorph, contains, degrees, detect, PianoMenuMorph,
document, getDocumentPositionOf, isNaN, isString, newCanvas, nop, parseFloat,
radians, useBlurredShadows, SpeechBubbleMorph, modules, StageMorph, Sound,
Morph, MorphicPreferences, Object, ScrollFrameMorph, ShadowMorph, ZERO, Sound,
String, StringMorph, TextMorph, contains, degrees, detect, PianoMenuMorph, nop,
document, getDocumentPositionOf, isNaN, isString, newCanvas, parseFloat, isNil,
radians, useBlurredShadows, SpeechBubbleMorph, modules, StageMorph, SymbolMorph,
fontHeight, TableFrameMorph, SpriteMorph, Context, ListWatcherMorph, Rectangle,
DialogBoxMorph, BlockInputFragmentMorph, PrototypeHatBlockMorph, WHITE, BLACK,
Costume, IDE_Morph, BlockDialogMorph, BlockEditorMorph, localize, isNil, CLEAR,
Costume, IDE_Morph, BlockDialogMorph, BlockEditorMorph, localize, CLEAR, Point,
isSnapObject, PushButtonMorph, SpriteIconMorph, Process, AlignmentMorph,
CustomCommandBlockMorph, SymbolMorph, ToggleButtonMorph, DialMorph*/
CustomCommandBlockMorph, ToggleButtonMorph, DialMorph, SnapExtensions*/
// Global stuff ////////////////////////////////////////////////////////
modules.blocks = '2021-June-11';
modules.blocks = '2021-June-14';
var SyntaxElementMorph;
var BlockMorph;
@ -352,6 +352,11 @@ SyntaxElementMorph.prototype.labelParts = {
'(4) triangle' : 4
}
},
'%prim': {
type: 'input',
tags: 'read-only static',
menu: 'primitivesMenu'
},
'%audio': {
type: 'input',
tags: 'read-only static',
@ -9322,6 +9327,16 @@ InputSlotMorph.prototype.messagesReceivedMenu = function (searching) {
return dict;
};
InputSlotMorph.prototype.primitivesMenu = function () {
var dict = {},
allNames = Array.from(SnapExtensions.keys());
allNames.sort().forEach(name =>
dict[name] = name
);
return dict;
};
InputSlotMorph.prototype.collidablesMenu = function (searching) {
var dict = {
'mouse-pointer' : ['mouse-pointer'],

101
src/extensions.js 100644
Wyświetl plik

@ -0,0 +1,101 @@
/*
extensions.js
additional primitives for SNAP!
written by Jens Mönig
Copyright (C) 2021 by Jens Mönig
This file is part of Snap!.
Snap! is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Global settings /////////////////////////////////////////////////////
/*global modules, List*/
modules.extensions = '2021-June-14';
// Global stuff
var SnapExtensions = new Map();
// exceptions:
SnapExtensions.set(
'error(msg)',
function (msg) {
throw new Error(msg);
}
);
// list utils:
SnapExtensions.set(
'sort(list, fn)',
function (data, fn, proc) {
return proc.reportAtomicSort(data, fn);
}
);
SnapExtensions.set(
'linked(list)',
function (data) {
return data.isLinked;
}
);
// text utils:
SnapExtensions.set(
'lowercase(txt)',
function (txt) {
return txt.toLowerCase();
}
);
// frequency distribution analysis:
SnapExtensions.set(
'analyze(list)',
function (list) {
var dict = new Map(),
result = [],
data = list.itemsArray(),
len = data.length,
i;
for (i = 0; i < len; i += 1) {
if (dict.has(data[i])) {
dict.set(data[i], dict.get(data[i]) + 1);
} else {
dict.set(data[i], 1);
}
}
dict.forEach(function (value, key) {
result.push(new List([key, value]));
});
return new List(result);
}
);
SnapExtensions.set(
'group(list, fn)',
function (data, fn, proc) {
return proc.reportAtomicGroup(data, fn);
}
);

Wyświetl plik

@ -84,7 +84,7 @@ BlockEditorMorph, BlockDialogMorph, PrototypeHatBlockMorph, BooleanSlotMorph,
localize, TableMorph, TableFrameMorph, normalizeCanvas, VectorPaintEditorMorph,
AlignmentMorph, Process, WorldMap, copyCanvas, useBlurredShadows*/
modules.objects = '2021-June-09';
modules.objects = '2021-June-14';
var SpriteMorph;
var StageMorph;
@ -1502,6 +1502,18 @@ SpriteMorph.prototype.initBlocks = function () {
spec: 'code of %cmdRing'
},
// Extensions
doApplyExtension: {
type: 'command',
category: 'other',
spec: 'primitive %prim %mult%s'
},
reportApplyExtension: {
type: 'reporter',
category: 'other',
spec: 'primitive %prim %mult%s'
},
// Video motion
doSetVideoTransparency: {
type: 'command',
@ -1514,7 +1526,7 @@ SpriteMorph.prototype.initBlocks = function () {
category: 'sensing',
spec: 'video %vid on %self',
defaults: [['motion'], ['myself']]
},
}
};
};

Wyświetl plik

@ -59,9 +59,10 @@ degrees, detect, nop, radians, ReporterSlotMorph, CSlotMorph, RingMorph, Sound,
IDE_Morph, ArgLabelMorph, localize, XML_Element, hex_sha512, TableDialogMorph,
StageMorph, SpriteMorph, StagePrompterMorph, Note, modules, isString, copy, Map,
isNil, WatcherMorph, List, ListWatcherMorph, alert, console, TableMorph, BLACK,
TableFrameMorph, ColorSlotMorph, isSnapObject, newCanvas, Symbol, SVG_Costume*/
TableFrameMorph, ColorSlotMorph, isSnapObject, newCanvas, Symbol, SVG_Costume,
SnapExtensions*/
modules.threads = '2021-June-10';
modules.threads = '2021-June-14';
var ThreadManager;
var Process;
@ -811,6 +812,23 @@ Process.prototype.evaluateBlock = function (block, argCount) {
}
};
// Process: Primitive Extensions (for libraries etc.)
Process.prototype.doApplyExtension = function (prim, args) {
this.reportApplyExtension(prim, args);
};
Process.prototype.reportApplyExtension = function (prim, args) {
var ext = SnapExtensions.get(prim);
if (isNil(ext)) {
throw new Error('missing / unspecified extension: ' + prim);
}
return ext.apply(
this.blockReceiver(),
args.itemsArray().concat([this])
);
};
// Process: Special Forms Blocks Primitives
Process.prototype.reportOr = function (block) {